How to do scanf for single char in C [duplicate] How to do scanf for single char in C [duplicate] c c

How to do scanf for single char in C [duplicate]


The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.

One way around the problem is to put a blank space before the conversion specifier in the format string:

scanf(" %c", &c);

The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.


First of all, avoid scanf(). Using it is not worth the pain.

See: Why does everyone say not to use scanf? What should I use instead?

Using a whitespace character in scanf() would ignore any number of whitespace characters left in the input stream, what if you need to read more inputs? Consider:

#include <stdio.h>int main(void){   char ch1, ch2;   scanf("%c", &ch1);  /* Leaves the newline in the input */   scanf(" %c", &ch2); /* The leading whitespace ensures it's the                          previous newline is ignored */   printf("ch1: %c, ch2: %c\n", ch1, ch2);   /* All good so far */   char ch3;   scanf("%c", &ch3); /* Doesn't read input due to the same problem */   printf("ch3: %c\n", ch3);   return 0;}

While the 3rd scanf() can be fixed in the same way using a leading whitespace, it's not always going to that simple as above.Another major problem is, scanf() will not discard any input in the input stream if it doesn't match the format. For example, if you input abc for an int such as: scanf("%d", &int_var); then abc will have to read and discarded. Consider:

#include <stdio.h>int main(void){    int i;    while(1) {        if (scanf("%d", &i) != 1) { /* Input "abc" */            printf("Invalid input. Try again\n");        } else {            break;        }    }    printf("Int read: %d\n", i);    return 0;}

Another common problem is mixing scanf() and fgets(). Consider:

#include <stdio.h>int main(void){    int age;    char name[256];    printf("Input your age:");    scanf("%d", &age); /* Input 10 */    printf("Input your full name [firstname lastname]");    fgets(name, sizeof name, stdin); /* Doesn't read! */    return 0;}

The call to fgets() doesn't wait for input because the newline left by the previous scanf() call is read and fgets() terminates input reading when it encounters a newline.

There are many other similar problems associated with scanf(). That's why it's generally recommended to avoid it.

So, what's the alternative? Use fgets() function instead in the following fashion to read a single character:

#include <stdio.h>int main(void){    char line[256];    char ch;    if (fgets(line, sizeof line, stdin) == NULL) {        printf("Input error.\n");        exit(1);    }    ch = line[0];    printf("Character read: %c\n", ch);    return 0;}

One detail to be aware of when using fgets() will read in the newline character if there's enough room in the inut buffer. If it's not desirable then you can remove it:

char line[256];if (fgets(line, sizeof line, stdin) == NULL) {    printf("Input error.\n");    exit(1);}line[strcpsn(line, "\n")] = 0; /* removes the trailing newline, if present */


This works for me try it out

int main(){char c;scanf(" %c",&c);printf("%c",c);return 0;}