Results 1 to 8 of 8

Thread: I have a little confusion in scanf function

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    I have a little confusion in scanf function

    Hi.

    I'm going ahead in my learning process of C. I came across with a topic called pointer. But before learning pointer, I have a confusion.

    Lets say this is a simple program which takes input from user and produce the output.

    Code:
    int a;
    printf("enter a number \n");
    scanf("%d", &a);
    Here, & is Address Of operator. which according to my beginner understanding is used to take the address of the variable a. Meaning, that the memory location where the number will store will be collected by the Address Of operator.

    If this is the case, I mean that if I am right, then if I used the printf like this
    Code:
    printf("%d", a);
    So the result is supposed to be "Address of variable a".

    But in actual, instead of address of variable a is display, the entered value is displayed on the screen.

    Why???

    Why the number user enters is displayed on the screen instead of Address of the a"??

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: I have a little confusion in scanf function

    Code:
    int a;
    printf("enter a number \n");
    scanf("%d", &a);
    Here, & is Address Of operator. which according to my beginner understanding is used to take the address of the variable a.
    Correct. In the case of scanf(), the entered value is placed in the specified memory which in this case is the memory used to store the value of a.

    Code:
    printf("%d", a);
    So the result is supposed to be "Address of variable a".
    No as just a is used and not &a - so the value of a is shown.

    But in actual, instead of address of variable a is display, the entered value is displayed on the screen.
    Because just a is used which gives the value of a and not &a which gives the address of a. If you want to see the address of a then use

    Code:
    printf("%p", &a);
    or for both

    Code:
    printf("The address of a is %p and its value is %d\n", &a, a);
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: I have a little confusion in scanf function

    I did not grasp your answer until I practice little more, but before my further practice, your answer raised another question in my mind.

    I am reading a book, and visited couple of sites today for the above-mentioned issue.

    You have mentioned "%p", in my book, the writer has used "%u" and two websites I visited one has used "%x" and one I forgot now.

    Now please guide me that for the same pointer, why different alphabets used? I mean format specifier used differently for pointer, why is it so?

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: I have a little confusion in scanf function

    %p is the format specifier when displaying a pointer - it displays as an address in hexadecimal with 8 digits 0 padded (the same as %08X format). For the various type specifiers that can be used see
    https://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx

    %u means unsigned integer displayed as a decimal and %x means display as hexadecimal. Depending upon how you want the display, depends upon what type format specified is used. All 3 are 'correct' but will provide different displays.

    Consider
    Code:
    int a = 123;
    
    printf("&a %%p:   %p\n&a %%08X: %08X\n&a %%u:   %u\n&a %%x:   %x\n a %%d:   %d\n a %%x:   %x", &a, &a, &a, &a, a, a);
    which on my system (yours will display different for the first four) displays
    Code:
    &a %p:   0015FEBC
    &a %08X: 0015FEBC
    &a %u:   1441468
    &a %x:   15febc
     a %d:   123
     a %x:   7b
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: I have a little confusion in scanf function

    I repeated my topic couples of times but still I have the same question?

    Code:
      printf("enter any number");
    scanf("%d", &a);
    This scanf is used here to store the user entered number into a memory location, which is labeled as "a". right.

    1). How it will store data into "a"?
    2). If this scanf is just storing data then
    why address of operator & is used?

    Thanks.

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: I have a little confusion in scanf function

    scanf stores the entered number to the memory address supplied. The required memory location is obtained by &a which provides the memory address at which the value for a is stored. a is the value of a, &a is the memory address at which that value is stored.

    I don't know how far through your c course you are, but the reason that &a is used and not a is that in c function parameters are passed by value. So if just
    Code:
    scanf("%d", a);
    was used, then the function scanf() would update its copy of a in its function but this wouldn't update the value of a in the calling function. In c for a function to update the value of a function argument, that argument has to be passed by address - not by value.

    If you are still having questions, I would suggest you wait until you have covered function parameter passing.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: I have a little confusion in scanf function

    The required memory location is obtained by &a which provides the memory address at which the value for a is stored
    1). Here obtained means that the scanf function is used to obtain memory location? Right???

    2). So If I am getting it right, so you want to say that by &a we get the address of that memory location which is labeled as a and we store the entered value at that particular location by using scanf function? Am I right?

  8. #8
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: I have a little confusion in scanf function

    So If I am getting it right, so you want to say that by &a we get the address of that memory location which is labeled as a and we store the entered value at that particular location by using scanf function? Am I right?
    Yes.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width