0

The prototype of printf and scanf in stdio.h are:

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

Seems like it accepts the same type of argument, that is const char * format. I understand that is a pointer to a constant char variable that has name "format". So both function accept format name as first variable. But the list of argument after ,... make me confused.

Take 2 example, we will see printf accept something like content of variable Example:

int a = 20;
int *pa = &a;
printf("%d", a);
printf ("%d", *pa);

scanf() accepts the address of variable:

Example:

int a =20;
int *pa = &a;
scanf("%d", &a);
scanf("%d", pa);

In short, it easy to accept that is the way these function work, but how I can find where they define, prototype in header file is not enough, I can try to google something like:"built a printf() for yourself", some books like "C programming language" also teach how to create some built-in function.

But I think there must have some file that saved all these functions.

kidman1670
  • 21
  • 3
  • "So I confuse with it" - In what way are you confused? – Ed Heal Jan 11 '20 at 10:23
  • Does this answer your question? [Parameter Passing in C - Pointers, Addresses, Aliases](https://stackoverflow.com/questions/29088971/parameter-passing-in-c-pointers-addresses-aliases) – Yunnosch Jan 11 '20 at 10:24
  • 1
    The implementations of `printf` and `scanf` are inside implementations of the standard C library, and they are generally very complicated. Quite likely, there is not “some file” that implements them, but several files, and, unfortunately, pointing you to those files is not likely to be great help in understanding how they are implemented. It might be better if you ask specific questions about them, such as “How does a function with varying argument types process its arguments?” or “How does `printf` compute the characters to display for `%d`?” or whatever specific things you want to know. – Eric Postpischil Jan 11 '20 at 13:24
  • @EricPostpischil thanks you very much, this is a truly helpful answer I want to find. – kidman1670 Jan 11 '20 at 14:37

3 Answers3

0

In short:

The printf function takes a set of values to print.

The scanf function takes a set of pointers to variables where to store the value it has read.

Some related things you might want to research includes variable argument functions (the whole ... argument thing), and how to emulate pass by reference in C.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • “Set” ought to be avoided in this context as it has a specific mathematical (and computer science) meaning that is not applicable to the arguments of `printf` or `scanf`. – Eric Postpischil Jan 11 '20 at 13:19
0

The const char* format argument is the first argument of the function -- the format string, which is "%d" in your examples.

The other arguments (a or pa in your examples) are varargs, denoted by the ... in the argument list of the function declaration. This allows a function to take a variable number of arguments.

This means that printf can take more arguments, like so:

int a = 1;
int b = 2;
printf("a=%d b=%d\n", a, b);

This would print a=1 b=2.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
0

The type of the first argument in both functions scanf() and printf() are the same, i.e. a string. The ellipsis (...) represents a variable length list of arguments and is referred to as varargs. The types of the varargs parameters are not the same. Hence both scanf() and printf() may take more than one argument.

scanf() will store the entered value into memory and hence requires a pointer, i.e. an address in memory so it knows where to store the entered value.

printf() displays a value that is already stored in a memory area.

So basically you could say that scanf() writes to a memory area whereas printf() reads from a memory area.

Abra
  • 19,142
  • 7
  • 29
  • 41