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).
Related
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
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Using scanf and fgets in the same program?
(4 answers)
Closed 5 months ago.
#include <stdio.h>
int main(void) {
int numWords;
char str[100];
printf("Enter how many words:\n");
scanf("%d", &numWords);
printf("Enter %d words:", numWords);
fgets(str, 100, stdin);
return(0);
}
This program does not read fgets at all. It will print the text and scan what what the user enters into the numWords, but will not read scanf. I have tried putting a newline character after scanf but then the "Enter %d words:" will not print. I would like everything to print in order.
This is just the beginning of a more complex program, not the whole thing.
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);
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".
This question already has answers here:
Why 2nd scanf doesn't work in my program?
(3 answers)
Closed 8 years ago.
I am try the code below.But when I input the integer,then program does not ask for character.Program execute the printf line.How should i avoid above problem?.
#include <stdio.h>
void main()
{
char a[5];
int p;
printf("data\n");
scanf("%d",&p);
scanf ("%c",&a);
printf("--> %c %d\n",a,p);
}
Put a space in scanf like this:
scanf (" %c",&a);
^-------note
So that the trailing newline is eaten up. Once you hit enter after giving the integer input - there is a trailing newline character in the buffer which the second call to scanf reads. Also main as per ISO should return int
Also this statement is incorrect:
char a[5];
printf("--> %c %d\n",a,p);
You are reading a char and printing an array. You simply need:
char a;
printf("--> %c %d\n",a,p);
If you want to read (or take input) array of chars then use fgets. For char a[5] do something like:
fgets (a, 5 , stdin)
Since fgets is buffer safe.