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

Click For Summary
SUMMARY

The printf function in C is a variadic function, allowing for a variable number of arguments, indicated by the ellipsis ("...") in its prototype. The first argument is typically a format string, which specifies the types of subsequent arguments using format specifiers such as %s for strings and %d for integers. The number of additional arguments must be known by the function, usually passed as a count or inferred from the format string. Understanding variadic functions is essential for effectively using printf and similar functions in C programming.

PREREQUISITES
  • Understanding of C programming syntax
  • Familiarity with function prototypes and definitions
  • Knowledge of format specifiers in C
  • Basic understanding of variable argument lists in functions
NEXT STEPS
  • Study the implementation of variadic functions in C using stdarg.h
  • Learn how to create custom variadic functions
  • Explore the use of format specifiers in printf and scanf functions
  • Investigate error handling in variadic functions
USEFUL FOR

Beginner C programmers, computer science students, and anyone interested in mastering function parameters and variadic functions in C.

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: 130
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 ..
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
7
Views
2K
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K