简体   繁体   中英

Use of scanf() in C

I have the following question. In the below code, if the input is a single character, then it gets printed successfully in output window. But if more than one character is given as input, it prints each character(both alphanumeric characters) separately until all are done.

#include<stdio.h>
#include<conio.h>
void main()
{
    char c;
    printf("enter any character: \n");
    while(scanf("%c",&c)!=0)
    {
        printf("%c\t",c);
    }
    getche();
}

Question: if a single character specifier ( %c ) is used in input function, then it must print only the first character and ignore the rest. But how its printing out all the input characters separately. Can anyone kindly clarify? Am unable to add image of output screen.

Once you enter multiple char s and press ENTER , all the char s are going to be stored in standard input. Running scanf("%c",&c) once will read one char and leave the rest of the inputs in the stdin . So, for the next time, when control reaches scanf("%c",&c) (due to the while() loop control), it will read the next char and so on, until the matching fails and scanf() returns a 0.

As mentioned in C11 , chapter §7.21.6.2 , the conditions for scanf() failure, which returns EOF ( in case of input failure before any matching takes place ) or 0 ( in case the matching fails, which is not possible for %c , anyway ) is

[...] If the length of the input item is zero, the execution of the directive fails; this condition is a matching failure unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.

TL;DR Input buffer does not auto-discard itself after a call to scanf() If you want the buffer to be clean before you call another scanf() , you need to do that yourself.

Having said that, void main() is not a standard signature of main() in hosted environment. You need to use int main(void) , at least.

The buffer (stdin) is not empty. You are fetching only one character, but doing it in loop. If you want to fetch only one character drop the while.

scanf function scans stdin buffer for the first occurance of specified template. In your case it's a single character. But the buffer is not cleared upon completion, so if you will call scanf once more - you will get the next character from the stream. This behavior can be avoided using special terminal configuration. See termios for examples.

Repeated execution of the code

scanf("%c", &c);

will read every character that is input - including the newline that you type. You can change it to ignore any preceding whitespace with

scanf(" %c", &c);

Note the difference is a space before the format specification. Apart from that, none of the input is ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM