0

Possible Duplicate:
What is the signature of printf?

I know that there will be an argument list for every function. if we pass the arguments more than no.of variables mentioned in the argument list then it gives an error.but my question is

Printf() function also has argument list and eventhough we give 'n' no.of arguments the printf() function doesnt fails then

i want to know wht will be used in argument list section of printf() which takes infinate argument list?

Community
  • 1
  • 1
Gouse Shaik
  • 340
  • 1
  • 2
  • 15

3 Answers3

5

printf is a "variadic" function. This means that the argument list is declared with ... on the end, and in the implementation of printf the va_list, va_start, va_arg etc macros are used to extract the arguments from the variable length list.

Vicky
  • 12,934
  • 4
  • 46
  • 54
  • (Also, technically, the argument list is not infinite - it's limited by the stack size.) – Vicky Sep 20 '11 at 12:05
4

printf's signature looks something like this:

int printf ( const char * format, ... );

If a function has a '...' as its last argument, it can receive any number of arguments. Within the function, you would use va_arg to access those arguments. Here is an example from cplusplus.com:

/* va_start example */
#include <stdio.h>
#include <stdarg.h>

void PrintFloats ( int amount, ...)
{
  int i;
  double val;
  printf ("Floats passed: ");
  va_list vl;
  va_start(vl,amount);
  for (i=0;i<amount;i++)
  {
    val=va_arg(vl,double);
    printf ("\t%.2f",val);
  }
  va_end(vl);
  printf ("\n");
}

int main ()
{
  PrintFloats (3,3.14159,2.71828,1.41421);
  return 0;
}

Notice that PrintFloats here requires that you pass in the number of additional arguments. printf doesn't need to do this, because it can infer how many arguments you are passing in by counting the tags in the format string.

Kevin
  • 74,910
  • 12
  • 133
  • 166
-3

The keyword is variadic arguments, and they are declared with ...

thiton
  • 35,651
  • 4
  • 70
  • 100
  • 1
    Are you planning to complete this or should we go ahead and downvote as it doesn't really answer the Question convincingly. – Alok Save Sep 20 '11 at 12:10
  • 1
    It is complete, please feel free to downvote. I think that there are plenty of explanations in the web for the keywords variadic arguments and that they need not be repeated here - this is why I kept my answer short. – thiton Sep 20 '11 at 12:20
  • -1 If the argument for posting an Incomplete answer is `plenty of explanations available on the web` then one should not bother to answer.If one cares to post an answer it better be correct and complete. – Alok Save Sep 20 '11 at 15:09
  • I respect your opinion about that, but differ about the completeness. By the way, I hope you are not taking the terminal "..." of my answer as an ellipsis, they reference the ... in "void foo(...)". – thiton Sep 20 '11 at 15:13
  • Since you feel the need to provide an explanation about what `...` stand for in your answer it provides a testimonial to the **completeness** of it. – Alok Save Sep 20 '11 at 15:15