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

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

Related

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.

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

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);

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

what is the wrong in using two scanf(%s) simultaneously [duplicate]

This question already has answers here:
scanf: "%[^\n]" skips the 2nd input but " %[^\n]" does not. why?
(6 answers)
Closed 6 years ago.
why second scanf doesnt take any input ? does %s cancatanae the string?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string1[50];
char string2[50];
scanf("%[^\n]s",string1);
scanf("%[^\n]s",string2);
printf("\nfirst string :%s \n",string1);
printf("\nsecnd string :%s \n",string2);
return 0;
}
input : this is bbc [enter]
output: first string :this is bbc
second string:{some characters symbol}
"%[^\n]s" tells to scanf to "eat" all the character that aren't a newline and put them in the argument; this means that, after the first scanf returns, it is leaving the newline that made it stop in the read buffer. The next scanf finds it and immediately stops reading - after all, you told it to read up to the first newline!
A solution here can be to use "%[^\n]s\n", which "eats" even the newline that follows the string, or even "%[^\n]s ", where the space is "magic", in that it tells scanf to eat all the other whitespace it can find, which includes newlines (notice however that this is a bad idea if you want to be able to read an empty string in the next line).
Even simpler, you can simply use fgets(string1, sizeof(string1), stdin), which, unlike scanf with %s, is safe against buffer overflows (yes, you can make even %s safe, but it's more work). Notice that fgets puts even the trailing newline into your string.
The problem is that your scanf format string requires the string to end in s: %[^\n] is interpreted as the format specifier, and then you have an s which must be matched by the input. Since the second string does not start in s, the second scanf reads nothing.
Replacing s with a space will fix the problem:
scanf("%[^\n] ", string1);
// ^
// Space is important
Even better, put space in front of %[^\n] in the second scanf:
scanf("%49[^\n]", string1);
scanf(" %49[^\n]", string2);
Since your string buffers have limited capacity, putting a limit of 49 is necessary to avoid buffer overruns.
Your code will only read one input. As %[^\n]s fails to read an 's'
Replace two scanf() statement with following one.
Just executed your code on my system it works.
scanf("%[^\n]s %[^\n]s",string1,string2);
Or just remove s from your statements and add space.
scanf("%[^\n] ",string1);
scanf("%[^\n] ",string2);
checked with your input for both the string and it displays the proper output.

Resources