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: 115
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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top