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

Discussion in 'C Programming' started by sushant, Jan 10, 2005.

  1. sushant

    sushant Guest

    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
     
    sushant, Jan 10, 2005
    #1
    1. Advertising

  2. sushant

    Mike Wahler Guest

    "sushant" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Jan 10, 2005
    #2
    1. Advertising

  3. 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.
     
    Jonathan Burd, Jan 10, 2005
    #3
  4. sushant

    Mike Wahler Guest

    Re: [corr] why to use '&' in scanf( ) but not in printf( )

    "Mike Wahler" <> wrote in message
    news:0VoEd.3209$...
    >
    > 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
     
    Mike Wahler, Jan 10, 2005
    #4
  5. sushant

    Chris Torek Guest

    In article <>
    sushant <> 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 (40°39.22'N, 111°50.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.
     
    Chris Torek, Jan 10, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. code_wrong

    Re: printf scanf problem

    code_wrong, Aug 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    422
    /dev/null
    Aug 2, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,449
    those who know me have no need of my name
    Apr 3, 2006
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    689
    Richard Bos
    May 2, 2006
  5. Meske Moday

    Weird usage of printf/scanf? Why do they work?

    Meske Moday, Sep 7, 2009, in forum: C Programming
    Replies:
    15
    Views:
    949
    Old Wolf
    Sep 8, 2009
Loading...

Share This Page