scanf ("%c", &a); Why does it need & before 'a'?

This is a discussion on scanf ("%c", &a); Why does it need & before 'a'? within the C Programming forums, part of the General Programming Boards category; Hello, I am a newbie to C programming (or programming in general). I was wondering what the logic behind the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    scanf ("%c", &a); Why does it need & before 'a'?

    Hello, I am a newbie to C programming (or programming in general).

    I was wondering what the logic behind the & in scanf() function is.

    For example,

    Code:
    scanf ("%c", &a);
    there is an & before a.

    As far as I am concerned &a means the 'address of a'. But here I am trying to assign 'a' to a character (a = 'k').

    Can anyone clarify this!

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You need the & because you want to modify your variable. If you didn't use it, a copy of the value of variable would be passed instead. Any assignment to that copy would not affect your variable that you declared.

    That's not to say that you can pass a variable of a type that is not a char * to scanf when it expects one. You can't. I was just saying what would happen if the function was designed like that.
    Last edited by robwhit; 05-17-2008 at 05:09 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2
    ohhh...... got it!

    Thanks

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    For some more in-depth. I hope you have a book, though?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21