Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to do scanf for single char in C [duplicate]

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

August 16, 2021 by James Palmer

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

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: %cn”, ch1, ch2);

/* All good so far */

char ch3;
scanf(“%c”, &ch3); /* Doesn’t read input due to the same problem */
printf(“ch3: %cn”, 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

int main(void)
{
int i;

while(1) {
if (scanf(“%d”, &i) != 1) { /* Input “abc” */
printf(“Invalid input. Try againn”);
} else {
break;
}
}

printf(“Int read: %dn”, i);
return 0;
}

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

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

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: %cn”, 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 */

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in