Possible Duplicate:
confusion in scanf() with & operator
Why we need a & in scanf for inputting integer and why not for characters.
Do the & in scanf refers to merory location while getting input.
Eg:-
main()
{
int a;
char c;
scanf("%d",&a);
scanf("%c"c);
}
For each conversion specifier,
scanf()expects the corresponding argument to be a pointer to the proper type:%dexpects an argument of typeint *,%fexpects an argument of typedouble *,%cand%sboth expect an argument of typechar *, etc.The difference between
%cand%sis that the former tellsscanf()to read a single character and store it in the location specified by the corresponding argument, while the latter tellsscanf()to read multiple characters until it sees a 0-valued character and store all those characters in the buffer starting at the location specified by the argument.You need to use the
&operator on your arguments if they are not already of pointer type. For example:Where things get confusing is reading strings of characters (using the
%sconversion specifier):Why don’t we need the
&operator in this case? It has to do with how C treats array expressions. When the compiler sees an expression of array type (such asbufin thescanf()call), it will implicitly convert the expression from typeN-element array of Ttopointer to T, and set its value to the address of the first element in the array. This value is not an lvalue — it cannot be assigned to (so you can’t write something likebuf = foo). The only exceptions to this rule are when the array expression is an operand of either thesizeofor&operators, or if the array expression is a string literal being used to initialize another array:In short, the expression
bufis implicitly converted from typechar [SIZE]tochar *, so we don’t need to use the&operator, and in fact the type of the expression&bufwould bepointer to SIZE-element array of char, or(*)[SIZE], which is not whatscanf()expects for the%sconversion specifier.