MHB What are the parameters for the printf function in the C language?

AI Thread Summary
The discussion centers on the concept of variadic functions in C, specifically focusing on the `printf` function. Variadic functions allow a variable number of arguments to be passed, indicated by the ellipsis ("...") in their prototype. The first argument typically specifies the number of subsequent arguments, which is essential because the function cannot inherently determine how many arguments were provided. For `printf`, this is achieved through format specifiers like %s and %d in the format string, which also dictate the types of the arguments. It is noted that at least one required argument must precede the ellipsis, preventing purely variadic functions without any fixed parameters. The discussion also touches on the interest in understanding the implementation of `printf` and `scanf`, with a request for tips on how to approach this.
Maged Saeed
Messages
123
Reaction score
3
As I know each function can have no parameter this function is called void function ,for example main function, also there is some functions which has one or more parameters either input or output , the number of parameters should be specified in the function prototype and function definition.

My question is that what is the printf function parameters since the number of parameters isn't specified? "I can input as much parameters as I want"I mean what is the prototype of the printf function or its definition ?

I have opened include library ,which printf function is included , but I found that there is three dots "..." what is that refers to

look at the picture of printf prototype!

Could anyone explain!View attachment 3607
 

Attachments

  • Untitled.png
    Untitled.png
    52.4 KB · Views: 114
Technology news on Phys.org
These types of functions are called variadic functions, because they have variable arity (vari-adic), and as you guessed, they exist to allow the caller to pass as many arguments as it wants to the function. The ellipsis (three dots "...") is the syntax used to denote a variadic function. For an example on how to use variadic functions, see this link. For instance, here is a variadic function (from the previous link) which takes a list of integers as arguments and returns their sum:

Code:
#include <stdarg.h>
#include <stdio.h>

int
add_em_up (int count,...)
{
  va_list ap;
  int i, sum;

  va_start (ap, count);         /* Initialize the argument list. */

  sum = 0;
  for (i = 0; i < count; i++)
    sum += va_arg (ap, int);    /* Get the next argument value. */

  va_end (ap);                  /* Clean up. */
  return sum;
}

int
main (void)
{
  /* This call prints 16. */
  printf ("%d\n", add_em_up (3, 5, 5, 6));

  /* This call prints 55. */
  printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

  return 0;
}

Note the first argument "count" to the function is the number of arguments that follow. This is because it's not possible for the function to deduce on its own the number of arguments that were passed. That information must be passed to the function separately, usually through a first required argument (here a "count" variable, for the printf family of functions this is derived through the number of format specifiers %s, %d, etc.. in the format string). The function also obviously does not know the type of its variadic arguments, which must either be known at compile-time or, again, passed separately (in this example, they are all assumed to be int, for printf the function looks at the format specifiers: %s for a string, %d for an int, etc.).

Also, due to C's syntax, at least one required argument must precede the ellipsis, so you cannot have "purely" variadic functions like "int foo(...);" in standard C. This is usually not a problem, as a purely variadic function does not seem very useful at first glance.

If you have any questions, please don't hesitate!​
 
I'm studying my first course in computer science , and I have a little information about programming at all , but that seems interesting
:o
I think now , it will be easier to give the printf and scanf bodies ..

Just could you give me some tips about how to do so ..
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top