How to do scanf for single char in C
Solution 1
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.
Solution 2
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 */
Solution 3
This works for me try it out
int main(){
char c;
scanf(" %c",&c);
printf("%c",c);
return 0;
}
Solution 4
Here is a similiar thing that I would like to share,
while you're working on Visual Studio you could get an error like:
'scanf': function or variable may be unsafe. Consider using
scanf_s
instead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS
To prevent this, you should write it in the following format
A single character may be read as follows:
char c;
scanf_s("%c", &c, 1);
When multiple characters for non-null terminated strings are read, integers are used as the width specification and the buffer size.
char c[4];
scanf_s("%4c", &c, _countof(c));
Solution 5
neither fgets nor getchar works to solve the problem. the only workaround is keeping a space before %c while using scanf scanf(" %c",ch); // will only work
In the follwing fgets also not work..
char line[256];
char ch;
int i;
printf("Enter a num : ");
scanf("%d",&i);
printf("Enter a char : ");
if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);
}
ch = line[0];
printf("Character read: %c\n", ch);
Yuval
Updated on July 10, 2022Comments
-
Yuval almost 2 years
In C: I'm trying to get char from the user with
scanf
and when I run it the program don't wait for the user to type anything...This is the code:
char ch; printf("Enter one char"); scanf("%c", &ch); printf("%c\n",ch);
Why is not working?
-
Ben Voigt over 9 yearsThis is the same suggestion John Bode gave two years ago.
-
ForceBru about 8 yearsThis doesn't answer the question because the OP wanted to
scanf
a single character, not a series of them. -
P.P over 7 years
fflush(stdin);
is undefined behaviour per C standard. -
danglingpointer over 6 yearsThis is not correct,
fflush(stdin)
is UB, mentioned in lots of question at SO. -
Hoppeduppeanut over 5 years"ch is not a valid variable for this program" Except it is? It's the first line of code in the given example.
-
tripleee almost 5 yearsHow exactly is this attempting to add value over older, existing answers?
-
edmanicom almost 5 yearsActually after reading the whole thread I would have to conclude there is no definitive answer here... just workarounds
-
Max Coplan almost 5 yearsNow that's wild! Would you explain why the space in front of
%c
makes a difference? -
Max Coplan almost 5 years
-
domsson over 4 yearsThis doesn't seem to add any more information than the accepted answer from 2012.
-
Besuglov Sergey over 4 yearsIt seems like it doesn't work for me. Unhandled exception at 0x799AF2F6 (ucrtbased.dll) in Deitel0805.exe: An invalid parameter was passed to a function that considers invalid parameters fatal. #include<stdio.h> #include<ctype.h> int main() { char c; printf("%s", "Please enter a character:"); scanf_s("%с", &c, 1); return 0; }
-
WENDYN about 4 yearsIt works with Microsoft compiler but that's about it
-
FaranAiki almost 3 yearsThis answer is very useful. I did not know that C compiler is not case-sensitive. For example:
THIS_ANSWER_IS_BAD
can be detected by C compiler asthis_answer_is_BAD
. Oh, it should bemychar
, notch
, tested it and it works.