Thread: What is the point of using the ampersand (&) on an int when using it in a scanf?

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    What is the point of using the ampersand (&) on an int when using it in a scanf?

    I'm just learning the basics of C right now and I'm really enjoying it! I am just curious as to why we need to use an ampersand inside of a scanf when trying to assign the typed value to an integer. I will provide an example now:

    printf("how many birds do u see daily?: \n");
    scanf("%d", &bird);

    This is not my whole code as this wouldn't compile by itself but I want to know why the program works with the ampersand and why it doesn't without it.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I believe scanf looks a bit like this:
    Code:
    int scanf(int* bird) {
        // read input stream
        // save next int into variable bird
        // return number of items read
    }
    while printf for example looks a bit like this:
    Code:
    int printf(int bird) {
        // print bird variable
        // return number of characters
    }
    A quick note: If you don't understand the return values of these functions, they don't apply to your question, so you can ignore that part.

    If a function is called that is to save data into a variable which survives the scope of the function, the address of that variable is required. printf doesn't need an address of a variable because it only needs a value to print.

    If the function scanf didn't receive a pointer to a variable and it only received the value, it wouldn't know where to save that value.

    In code form, if scanf looked like this:
    Code:
    int scanf () {
        int bird;
        // read input stream
        // save next int into variable bird
        // return number of items read
    }
    bird could not be used outside of the scanf function.

    And if scanf looked like this:
    Code:
    // read bird from input stream
    int scanf (int bird) {
        int value;
        // save bird into variable value
        // return ???
    }
    well, if you are being relied upon to direct a value of the input stream into the variable bird prior to a scanf call, what is the point of scanf anyway?

    I did honestly try to simplify this a bit to get across the answer to your question. I used comments instead of real code and ignored the formatting portion of the functions.
    Last edited by jack jordan; 12-12-2017 at 09:39 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    scanf needs the address of the variable.
    & is the "address-of" operator.

    The reason for this will become apparent when you learn about pointers.

  4. #4
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Jack, that was a really great way of putting it. Easy to understand for coding newbies like myself

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    Jack I find your explanation very enlightening. Keep up the good work. Guys like you make this much easier for those of us who are new to c. Thanks for your explanation i learnt something new today.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable memory location with ampersand vs pmap
    By setevoy in forum C Programming
    Replies: 1
    Last Post: 10-23-2014, 01:37 PM
  2. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  3. don't quite get the ampersand '&'
    By CHanabi in forum C Programming
    Replies: 5
    Last Post: 02-26-2011, 03:20 PM
  4. execvp and ampersand?
    By slevytam in forum C Programming
    Replies: 16
    Last Post: 02-02-2005, 08:36 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread