Thread: don't quite get the ampersand '&'

  1. #1
    Registered User CHanabi's Avatar
    Join Date
    Feb 2011
    Posts
    1

    Unhappy don't quite get the ampersand '&'

    I don't quite get the ampersand '&' as in what is its function in the scanf/printf functions.

    In the below code shows the prinf function printing out the char variable c without the ampersand.
    Code:
    # include <stdio.h>
    void main(void)
    {
        char c='c';
        printf("The character is %c", c); // this prints 'c'
        return;
    }
    The next code is the same as the above code but instead has the ampersand in the printf function.
    Code:
    # include <stdio.h>
    void main(void)
    {
        char c='c';
        printf("The character is %c", &c); // this prints an upside down triangle
        return;
    }
    Another code shown below stores and prints a string but without the ampersand in both the printf and the scanf.
    Code:
    # include <stdio.h>
    void main(void)
    {
        char string[10];
        scanf("%s", string); // Let's say I input 'Hello!'
        printf("Message: %s", string); // this prints 'Hello!'
        return;
    }
    This code has the ampersand in the scanf.
    Code:
    # include <stdio.h>
    void main(void)
    {
        char string[10];
        scanf("%s", &string); // Let's say I input 'Hello!'
        printf("Message: %s", string); // this prints 'Hello!'
        return;
    }
    When printing strings, whether the ampersand is present or not doesn't make a difference. And when dealing with char and int variables, the presence of the ampersand makes a difference. So I don't really get the ampersand '&' and would gladly appreciate it if someone could explain it to me

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    printf() needs a value to print, scanf() needs an address where to put the variable. scanf() needs to communicate a variety of data types back to the caller, best way to do that is by assigning a value to an address that was passed in by the caller and let it worry about the rest.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    printf() doesn't need an ampersand anywhere in it, unless you request an address of a variable, to be printed. %p

    scanf() will always need an ampersand, unless the variable already is the address of the variable it is about to change. Change being the key word here - you need that address to make the change.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think your bottom line confusion is on arrays and pointers and how the compiler treats the array as a pointer to its first element when passed as a parameter. I also have a feeling you don't really understand what & means ( address-of). For more information you should read this:

    Cprogramming.com FAQ > Pointers And Arrays (intermediate)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    The ampersand in front of a variable represents the memory address where the variable is stored. So printf("%c", &x) prints the ASCII character assigned to the value of the memory address where x is stored. Since &x is probably "0xb880e8" or something, that printf statement is going to print something funky.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Compare these

    Code:
    char string[] = "hello world\n";
    printf("The string=%s", string );
    printf("The string=%s", &string[0] );
    Now increment 0 to say 1, and see what happens...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread