why to use '&' in scanf( ) but not in printf( )

S

sushant

hi

why do we use '&' operator in scanf like scanf("%d", &x); but why not
in printf() like printf("%d" , x);

thnx in advance
sushant
 
M

Mike Wahler

sushant said:
hi

why do we use '&' operator in scanf like scanf("%d", &x); but why not
in printf() like printf("%d" , x);

'printf'()' only need the values in order to output them.
'scanf()' stores values, so it needs a place to store them.
This is done by providing the addresses (in pointers) of
where to store the values.

Remember that C's 'calling convention' is by value.

void foo(int arg)
{
++arg;
}

void goo(int *arg)
{
++*arg;
}

int main()
{
int i = 42;
foo(i);
/* now 'i' is still 42 */
foo(&i);
/* now 'i' is 43 */
return 0;
}


-Mike
 
J

Jonathan Burd

sushant said:
hi

why do we use '&' operator in scanf like scanf("%d", &x); but why not
in printf() like printf("%d" , x);

thnx in advance
sushant

C only passes arguments by value. Some people might go a step
further by saying C passes arguments by value and by address.
Technically, an address is just another value, so it is actually
passing arguments by value. C has no notion of passing arguments
by reference (although some course books do claim so).

If you want a function to change the contents of a variable
that is not local to the function, you pass the address of the
variable to the function, so it knows where to look for
that particular variable in memory and then it can make changes
there.

scanf() reads values into variables (that are not local to scanf()),
so you have to pass the addresses of the variables you want
those values to be in. printf() does not change the contents of the
variables you pass to it, so you simply pass the values of the
variables.

Regards,
Jonathan.
 
M

Mike Wahler

Mike Wahler said:
Remember that C's 'calling convention' is by value.

void foo(int arg)
{
++arg;
}

void goo(int *arg)
{
++*arg;
}

int main()
{
int i = 42;
foo(i);
/* now 'i' is still 42 */
foo(&i);

Should be:

goo(&i);
/* now 'i' is 43 */
return 0;
}

Sorry for the error.

-Mike
 
C

Chris Torek

why do we use '&' operator in scanf like scanf("%d", &x); but why not
in printf() like printf("%d" , x);

In addition to the other (correct) answers about scanf()'s need for
a pointer to any given variable in order to make a change to that
variable's value, note that printf() and scanf() are not symmetric
in the first place. That is, there are directives for printf() that
mean something quite different in scanf(), and vice versa. I
mention this because the names and other similarities make people
think they "work the same", but they do not.

Consider, for instance:

int width, maxlen;
char *str;
... set up the variables ...
printf("%*.*s\n", width, maxlen, str);

which prints a string in the given field width, using at most
"maxlen" characters of the string. For scanf(), however, "%*s"
does not mean "find a width" but rather "suppress assignment",
and to set a maximum length you must use a literal numeric
value:

char buf[100];
int ret;
...
ret = scanf("%99s", buf);

Here scanf() will write at most 100 characters into buf[], and if
ret is not 0 or EOF (and 0 just happens to be impossible), at least
two. The last written byte will be the '\0' that terminates a C
string.

On the other hand, with printf() you can even supply an ordinary
array that is not '\0'-terminated:

char s[4] = { '1', '2', '3', '4' };
...
printf("%.4s\n", s); /* prints "1234", even though s has no '\0' */

So: printf() %s can handle arrays without '\0', but scanf() %s
always produces arrays with '\0'; %* in printf() means field-width
but %* in scanf() means suppress assignment; printf() takes %f to
print float or double, but scanf() takes %f to read float and %lf
to read double; and so on. Despite surface similarities, these
are as different beasts as a house cat and a Siberian tiger.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top