Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

arrays - Scanning with %c or %s

I had to do a program for college in which I should separate, between a certain amount of people, those who liked and the ones who disliked something. so I did this:

    char like[100];

    printf("Like? Y or N 
");
    scanf ("%c", like);

The program compiled, but didn't work the way it should. The user was not able to write "y or n" when asked "Like?"

So I tried this:

    char like[100];

    printf("Like? Y or N 
");
    scanf ("%s", like);

And it worked. But I don't know why it worked. Can somebody please explain me the difference between %c and %s in a scanf?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, please do some basic research before coming here - questions like this can usually be answered with a quick Google search or checking your handy C reference manual.

char inputChar;              // stores a single character
char inputString[100] = {0}; // stores a string up to 99 characters long

scanf( " %c", &inputChar );  // read the next non-whitespace character into inputChar
//            ^ Note & operator in expression
scanf( "%s", inputString );  // read the next *sequence* of non-whitespace characters into inputString
//           ^ Note no & operator in expression

You would use %c when you want to read a single character from the input stream and store it to a char object. The %c conversion specifier will not skip over any leading whitespace, so if you want to read the next non-whitespace character, you need a blank before the %c specifier in your format string, as shown above.

You would use %s when you want to read a sequence of non-whitespace characters from the input stream and store them to an array of char. Your target array must be large enough to store the input string plus a terminating 0-valued character. The %s conversion specifier skips over any leading whitespace and stops reading at the first whitespace character following the non-whitespace characters.

Both %c and %s expect their corresponding argument to have type char * (pointer to char); however, in the first case, it's assumed that the pointer points to a single object, whereas in the second case, it's assumed that the pointer points to the first element of an array. For inputChar, we must use the unary & operator to obtain the pointer value. For inputString, we don't, because under most circumstances an expression of type "array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

Your code works fine as it is, but it's a bit confusing to read a single character and store it to an array.

Using %s without an explicit field width is risky; if someone types in more than 100 non-whitespace characters, scanf will happily store those extra characters to memory following inputString, potentially clobbering something important. It's generally safer to write something like

scanf( "%99s", inputString ); // reads no more than 99 characters into inputString

or to use fgets() to read input strings instead:

fgets( inputString, sizeof inputString, stdin );  

Please check §7.21.6.2 of the online draft of the C language standard for a complete description of all of the conversion specifiers for the *scanf functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...