This question already has answers here:
scanf() only sign and number
(2 answers)
Closed 7 years ago.
I need to parse input like this: "+ 704"
Into: switcher = "+" and c = 749
I got this:
scanf("%c %d", &switcher, &c);
This does not work.
Scanf returns 1 instead of 2, c = 4196080 and printf("%c", switcher) prints a newline.
What am i missing?
So basically, there's a newline waiting on the buffer. To get rid of it, simply add one space to the beginning of your formatting string.
scanf(" %c %d", &switcher, &c);
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:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 1 year ago.
Can someone please take a look and advise why the program does not move forward after the scanf gets a char(&), but waits for another char to be entered?
char user_char, hist_axis;
int axis_char_ok = 0,grade, max_count=0,j,k,m=1,grades[9] = {0};
printf(" Please enter a character: \n");
scanf(" %c ", &user_char);
The space following %c will eat up as much white space as possible, so it won't stop until you enter something that isn't a space or a line break (or an end-of-file).
If you change " %c " to " %c" then the problem should go away.
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:
scanf() leaves the newline character in the buffer
(7 answers)
C skipping one command of a function? [duplicate]
(2 answers)
Closed 6 years ago.
I've been given the pretty simple task of writing a program that will take two characters and then print the letters inbetween them using a for() loop.
Here's my code:
#include <stdio.h>
int main() {
char a, b;
printf("\nEnter the first character: ");
scanf("%c", &a);
printf("\nEnter the second character: ");
scanf("%c", &b);
for(char i = a; i <= b; i++) {
printf("%c ", i);
}
return 0;
}
When I run it, I am prompted to enter the first character correctly but when I press enter it only runs the next printf() and then terminates.
No errors or warnings or anything on compilation. Another similar question I found that was apparently solved does not work for me either.
Thanks in advance.
You have to consume the \n in stdin left by first scanf.
Fastest fix
scanf(" %c", &b);
The space before %c tells to scanf to ignore all whitespaces before to read the char.
If I read your code correctly, by pressing enter, you would enter the second character, which would most probably (depending on the environment) start with a numeric value of 13, which would be smaller than any letter, so the loop's body is executed only once.
This question already has answers here:
How to do scanf for single char in C [duplicate]
(11 answers)
Closed 8 years ago.
I have problem with this code:
printf("Select your math: \n'+'addition \n'-'subtraction \n'*'multiplication \n'/' division \n");
char do_math;
scanf("%c", &do_math);
printf("Type 1 st number: ");
Problem is, that program doesn't wait until I type "do_math" but it displays "Type 1 st number: " right after first printf. Any ideas?
The only way your compiler will miscompile that is if you deliberately defined scanf() as a do-nothing macro. Don't blame your compiler!
There is probably a previous scanf() call in the program that left a carriage return in the input buffer. You can confirm that hypothesis by printing the value of do_math.
Try scanf(" %c", &do_math); (with a space before %c) to discard such whitespace.
(It's also a good idea to pay attention to the return value from scanf().)