0
votes

scanf() needs a pointer as argument but printf doesn't. printf just needs the variable's name and it somehow seems to obtain the value stored in the memory location of the variable. Why wasn't scanf made to behave in the same way by resolving the variable name into the memory location (the same way printf works) and storing the value in it?

  1. What is the difference between printf("%d",&var) and printf("%d",var)
  2. What is the difference between printf("%p",&var) and printf("%p",var)
4
printf doesn't know about the variable's name, it just gets a copy of the value. scanf needs to store a value, so it must know where.Daniel Fischer
You can write printf("%d", 2 * 3); There's no variable at all!Raymond Chen
Function arguments are passed by value in C.Kerrek SB
Thanks I get it now. So when calling printf, the calling function sends the value of the variable (in case variable is used as argument). In case of scanf, pointer (again a value) is sent.aste123

4 Answers

5
votes

C is a pass-by-value language. If you were to write:

int x;
scanf("%d", x);

scanf would have no idea where to put the scanned result - you need to pass it a pointer to a memory location in which to store the scanned value:

scanf("%d", &x);

You could also make an explicit pointer variable, if that helps you to understand it:

int *y = &x;
scanf("%d", y);

Conversely, printf doesn't care where the value is located, just what the value happens to be. So passing the value:

printf("%d", x);

is just fine. As to your specific questions (assuming int var;):

What is the difference between printf("%d",&var) and printf("%d",var)

Strictly speaking, the first causes undefined behaviour and the second one prints the value of var. In practice, a lot of implementations will print the address of var in the first case.

What is the difference between printf("%p",&var) and printf("%p",var)

This case is just the opposite. The first one prints the address of var, and the second causes undefined behaviour. In practice, it will probably print the value of var, but you shouldn't rely on that. To be really correct, the first one should be printf("%p", (void *)&var);, too, but I've never seen an implementation where what you have written there wouldn't work fine.

0
votes

In the first case you are printing the address of var. In the second case the contents of val.
&val is the address of val.
scanf gets the address (pointer) in order to get the value

0
votes

Because scanf() needs to modify its arguments. And, since in C, there is no "pass by reference" semantics for arguments, only pass by value (that essentially means that the value of each argument is copied when passed to a function), thus, only pointers can be used to let functions modify their arguments.

What is the difference between printf("%d", &var) and printf("%d", var)?

The first one always invokes undefined behavior, the second one doesn't if var is an int, short or char.

What is the difference between printf("%p", &var) and printf("%p", var)?

The first one always invokes undefined behavior, the second one doesn't if var is a void *.

0
votes

You pass variables to printf by value, that's all printf needs to know, it is only supposed to print the values of variables (or expressions). But this is not enough for scanf, which needs to modify memory occupied by the variables where you want to put the data read from e.g. standard input. That's why scanf needs address of the memory occupied by the variables - there is no other way to modify the variables.