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: 112
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 ..
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top