How to make scanf for a single char in C

In C: I'm trying to get char from a user using scanf , and when I started it, the program did not wait for the user to type something ...

This is the code:

 char ch; printf("Enter one char"); scanf("%c", &ch); printf("%c\n",ch); 

Why does not it work?

+64
c char scanf
Nov 24
source share
9 answers

The conversion specifier %c will not automatically skip any leading spaces, so if a fuzzy new line is used in the input stream (for example, from a previous record, for example, in the case of a previous record), the scanf call will consume it immediately.

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

 scanf(" %c", &c); 

An empty value in the format string tells scanf skip leading spaces, and the first character without spaces will be read using the %c conversion specifier.

+179
Nov 24
source share

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 space character in scanf() will ignore any number of whitespace 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 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 3rd scanf () can be fixed in the same way using leading spaces, it will not always be as simple as above. Another serious problem: scanf() will not discard the input stream in the input stream if it does not match the format. For example, if you enter abc for int , for example: scanf("%d", &int_var); then abc will have to read and discard. 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 fgets() call does not wait for input because the newline left by the previous scanf () call is read, and fgets () completes the data entry when it encounters a new line.

There are many other similar problems related to scanf() . This is why it is generally recommended to avoid this.

So what is the alternative? Use fgets() as follows 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 consider when using fgets() will be to read a newline if there is enough space in the inut buffer. If this is not desired, 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 */ 
+8
03 Dec '16 at 15:02
source share

Before scanf places fflush(stdin); to clear the buffer.

+1
Jun 30 '14 at 18:26
source share

Here is a similar thing that I would like to share,

while you are working on Visual Studio, you may receive an error such as: "scanf": a function or variable may be unsafe. Instead, consider using scanf_s. To disable obsolescence, use _CRT_SECURE_NO_WARNINGS

To prevent this, you should write it in the following format

One character can be read as follows:

 char c; scanf_s("%c", &c, 1); 

When multiple characters are read for lines with a null terminating character, integers are used as the specification of the width and size of the buffer.

 char c[4]; scanf_s("%4c", &c, _countof(c)); 

Hello,

+1
Jan 13 '15 at 8:46
source share

Provides a space before the% c conversion specifier so that the compiler ignores spaces. A program can be written as follows:

 #include <stdio.h> #include <stdlib.h> int main() { char ch; printf("Enter one char"); scanf(" %c", &ch); /*Space is given before %c*/ printf("%c\n",ch); return 0; } 
0
Oct 11 '14 at 15:03
source share

try using getchar (); instead

Syntax:

 void main() { char ch; ch = getchar(); } 
0
Oct 01 '17 at 15:29
source share

It works for me, try

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

Use string instead of char as

 char c[10]; scanf ("%s", c); 

I believe that it works well.

-2
Jun 22 '16 at 16:47
source share

It suits me:

  #include <stdio.h> int main (void) { char a; scanf ("%c", &a); printf ("%c\n", a); return 0; } 

I don't know why your code is not working. I am using Codelite with gcc

-four
Nov 28 '14 at 12:56 on
source share



All Articles