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

In summary, the printf function takes a string as its first argument, and a list of integers as its second argument. The ellipsis (three dots "...") in the printf prototype indicates that the function can take as many arguments as are needed.
  • #1
Maged Saeed
123
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: 81
Technology news on Phys.org
  • #2
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!​
 
  • #3
I'm studying my first course in computer science , and I have a little information about programming at all , but that seems interesting
:eek:
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 ..
 

FAQ: What are the parameters for the printf function in the C language?

What is the "C Language | printft Parameter"?

The "C Language | printft Parameter" is a function in the C programming language that is used to print formatted output to the screen or other output device.

How is the "C Language | printft Parameter" used?

The "C Language | printft Parameter" is used by passing a format string and any desired arguments to the function. The function then prints the formatted output to the screen or other output device.

What is the format string in the "C Language | printft Parameter"?

The format string in the "C Language | printft Parameter" is a string of characters that specifies the format of the output. It can include placeholders for the arguments passed to the function and special formatting characters.

What are the arguments in the "C Language | printft Parameter"?

The arguments in the "C Language | printft Parameter" are the values that are passed to the function and are used to fill in the placeholders in the format string. They can be variables, constants, or expressions.

What are some common examples of using the "C Language | printft Parameter"?

Some common examples of using the "C Language | printft Parameter" include printing strings, numbers, and characters to the screen, formatting output in a specific way, and displaying variables and their values for debugging purposes.

Back
Top