In this super easy program written in C won't let me read a variable of type "char"? [duplicate] - c

This question already has answers here:
can not read character from user [duplicate]
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
This easy code in C won't work, and I don't get it why. If I read only "n", or only "ch" separately, it works, otherwise if I try to read them both, it won't let me read "ch". What happens and how could I make it work?
#include <stdio.h>
int main()
{
int n;
char ch;
printf("n=");
scanf("%d",&n);
printf("ch="); //when i press Build and Run it won't let me read "ch"??? why?
scanf("%c",&ch);
return 0;
}

When you read in a number using %d, a newline is left in the input buffer. When you then read a character with %c, it reads that newline immediately so you don't get prompted for more input.
Unlike the %d format specifier, which discards any leading whitespace, the %c format specifier does not.
Add a leading space before %c to consume any leftover whitespace:
scanf(" %c",&ch);

Related

C program won't use printf after scanf [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 2 days ago.
I am new to C and am writing a very simple C program which just provides a input and repeats it back to you. The code shows the first print f which is a $ and lets you input something but will not print the text back to you. Here is the code:
char input[50];
printf("$");
scanf("%s\n", input);
printf("\n %s", input);
I thought it might have been a compile issue but nothing changes. I use make token which is the name of the file.
Remove the "\n" in the scanf() format string. The trailing "\n" instructs scanf() to match any number of white space characters and it will only know when it's done when encountering a non-white space character. Meanwhile you expect it to return after reading the first "\n". Consider using fgets() instead.
#include <stdio.h>
int main() {
char input[50];
printf("$");
scanf("%s", input);
printf("\n %s", input);
}
and example session:
$abc
abc

Is it possible to use scanf() and getchar() in the same program to get input? [duplicate]

This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 1 year ago.
I'm struggling on a question proving scanf() and getchar() can both retrieve a character from the input.
However, when I try to put them inside the same program, only the first function is running properly. The latter is discarded completely.
#include <stdio.h>
char letter;
int main()
{
printf("I'm waiting for a character: ");
letter = getchar();
printf("\nNo, %c is not the character I want.\nTry again.\n\n",letter);
printf("I'm waiting for a different character: ");
scanf("%c",&letter);
printf("Yes, %c is the one I'm thinking of!\n",letter);
return(0);
}
output
I have tried switching the places of those two functions but it is of no use.
Can someone help me find the issue and provide a way to fix this? The only requirement is that the program takes input twice, once by the getchar() function and once via scanf()
The second read attempt just reads whitespace (the end of line character, since you pressed enter after the first letter). Simply replace it with this:
scanf(" %c", &letter);
The space before % will tell scanf to read the next non-whitespace character.

String cannot be entered [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
After printing "Second text", fgets expect from me to enter a string but a program is always being stopped. That happens when I try to enter a char by scanf or getchar. What's happening?
#include <stdio.h>
int main()
{
char c[100],cc;
int x;
printf("First text\n");
scanf("%d",&x);
printf("Second text\n");
fgets(c,100,stdin);
//scanf("%c",&cc);
//cc=getchar();
printf("\n %s %d",c,x);
}
You probably press "enter" after having entered a number; scanf will then read the number, but will leave a '\n' (i.e. the newline represening "enter") in the buffer; This will be treated as an "empty" line then by gets. (BTW: use fgets instead of gets).
To overcome this enter the number and the text seperated by a space in a single line (i.e. without a newline in between).

I got an error reading a character on C [duplicate]

This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 5 years ago.
I want to read a single character from the console, but when I do, the program reads characters yet and I must write another character to save the first and finish its execution.
Code:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
char peps;
int main(int argc, char const *argv[]) {
printf("write a character:\n");
scanf(" %c\n", &peps);
printf("%c\n", peps);
return 0;
}
Can anyone explain why it does that and how to correct this error?
If you remove the \n from the scanf(), it should work as you want.
ie, do
scanf(" %c", &peps);
instead of
scanf(" %c\n", &peps);
This is because the \n in the scanf() format string is telling the computer to read and ignore all white spaces (including \n) after reading a character.
So all white spaces including the newlines given by typing the enter key, would be ignored. This will stop only when a non-white space character is encountered which won't be read and would thus remain in the input buffer.
So, in your case, a character would first be read and it would wait for a non-white space character before executing the printf() following the scanf(). The non-white space character would remain in the input buffer and was not read and is hence not printed at once. It would be read only upon reading from the stdin again.
If you want to explore this further, consider placing that scanf() and printf() in a loop and examine the output.
Note that replacing that \n with a space would have the same effect.
ie,
scanf(" %c\n", &peps);
and
scanf(" %c ", &peps);
would have the same behavior.
What's the behavior of scanf when the format string ends with a newline?
Behaviour of scanf when newline is in the format string

Character is not accepted [duplicate]

This question already has answers here:
C skipping one command of a function? [duplicate]
(2 answers)
Closed 7 years ago.
I've written a C program to count the number of occurrences of an entered character. When I execute,I could only enter the file name.Before I enter the character to be counted , the next statement executes.The output I get is shown below.
Enter the file name
test.txt
Enter the character to be counted
File test.txt has 0 instances of
Here is my code
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1;
int cnt=0;
char a,ch,name[20];
printf("Enter the file name\n");
scanf("%s",name);
//fp1=fopen(name,"r");
printf("Enter the character to be counted\n");
scanf("%c",&ch);
fp1=fopen(name,"r");
while(1)
{
a=fgetc(fp1);
if (a==ch)
cnt=cnt+1;
if(a==EOF)
break;
}
fclose(fp1);
printf("File %s has %d instances of %c",name,cnt,ch);
return 0;
}
How to resolve this?
It's a common beginners problem, especially when using the scanf family of functions.
The problem is that when you enter the file-name, and press Enter, the scanf function reads the text but leaves the Enter key, the newline, in the input buffer, so when you next read a character it will read that newline.
The solution is very simple: Tell scanf to read and discard any leading white-space by putting a single space in the scanf format:
scanf(" %c",&ch);
// ^
// |
// Note leading space
If you follow the link to the scanf reference it will tell you that almost all formats automatically reads and discards leading white-space, one of the three exceptions is just the format to read single characters, "%c".

Resources