Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why to use '&' in scanf( ) but not in printf( )

Reply
Thread Tools

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

 
 
sushant
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-10-2005

"sushant" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Jonathan Burd
Guest
Posts: n/a
 
      01-10-2005
sushant wrote:
> 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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-10-2005
"Mike Wahler" <(E-Mail Removed)> wrote in message
news:0VoEd.3209$(E-Mail Removed) ink.net...
>
> 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


 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      01-10-2005
In article <(E-Mail Removed) >
sushant <(E-Mail Removed)> wrote:
>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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why I can NOT use Python but PHP Lad Python 2 12-02-2004 07:37 PM



Advertisments