C: Problems with scanf and printf - c

I'm having issues with accepting user input and the printing its ascii value in C. I'm tasked with writing a program that simply takes a single char as input and prints out its ascii value, and only stops when the user inputs 0 (the ascii value of 0 is 48). My problem is that if the the printf seems to function one loop behind scanf.
while(x == 1){
scanf("%c\n",&thisChar);
ascii = thisChar;
if(ascii == 48){
x = -1;
}
printf("Ascii: %d\n", ascii);
}
For example, when I run this from the command line, I get something like this:
f
0
Ascii: 102
f
Ascii: 48
and then the program ends. With those same inputs, I want the output to be:
f
Ascii: 102
0
Ascii: 48
and then end there. What is the error in my logic?

\n character is the root cause of your problem.
Change
scanf("%c\n",&thisChar);
to
scanf(" %c",&thisChar);
EDIT: OP asked why a space before %c in scanf matters in output?
When you inputs the data to a program and press Enter key, an extra character \n is passed to the input buffer along with the input data. For Ex: If you want to enter f in your program then on pressing Enter key, input buffers contains f\n. On first iteration of loop, character f is read by scanf leaving behind the \n in the buffer. On next iteration of loop, this \n is read by the scanf causing unexpected output.
To solve this issue you need to consume this \n before next read. Placing a space before %c specifier in scanf can consume any number of new-line characters.

Did you consider using just getchar(3) ? (Perhaps fflush(3) could be needed)... Also stdin in a terminal is a complex beast. See tty demystified.
The kernel (not only the libc) is sort of buffering each tty lines (see line discipline).
See also ncurses and GNU readline.

Dont use \n in scanf() .
Remove "\n" and execute

Related

How to read a scanf for multiple integer and string inputs until EOF?

I need to do scanf function like that: scanf("%s%d%d%s%d%d%s%d%d", X, &LO, &HI, Y, &LO2, &HI2, Z, &LO3, &HI3) and I want whenever user hit ctrl+D to end the scanning and turn off the program.
For example:
l 7 9 c 3 8 v 4 ctrl+D //input
--> end of scanning and the end of program.
Could you please help me to do that? I am a total beginner in programming..
For a Unix type terminal, Ctrl-D is usually the termios VEOF character, and is not passed through to standard input when the terminal is in canonical input mode. In that case, typing the Ctrl-D character at the beginning of a line will make scanf see an end-of-file condition, but it needs to be at the beginning of a line, not partway through a line as in your example. So you may need to type a Carriage Return before the Ctrl-D in order to return from scanf early.
You can check the return value of scanf to see if it read the required number of input values, and end the program if the return value is incorrect.

C printf wont print before scaning next number

I got this piece of code
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
int number, S;
float MO;
S = 0;
for(i=0;i<10;i++)
{
printf("AAA %d\n", i );
scanf("%d\n", &number);
S = S + number;
}
MO = S/10;
printf("%d , %f \n",S , MO );
return 0;
}
when the execution starts, AAA 0 is printed.I then give my first number.After that, i am expecting to see AAA 1 , but this will be printed only after i give my second number.
Checked this here
C/C++ printf() before scanf() issue
but seems i can get none of these solutions work for me
The answers claiming that this has something to do with flushing input or output are wrong. The problem has nothing to do with this. The appearance of the \n character at the end of the scanf() template string instructs scanf() to match and discard whitespace characters. It will do so until a non-whitespace character is encountered, or end-of-file is reached. The relevant part of the C11 Standard is §7.21.6.2 5:
A directive composed of white-space character(s) is executed by
reading input up to the first non-white-space character (which remains
unread), or until no more characters can be read. The directive never
fails.
In OP's case, a second number must be placed in the input stream so that the first can be assigned. The second number then remains in the input stream until the next call to scanf(). In the case of the example given by Stephan Lechner, taking input from a file, there is a number in the input stream after each number to be assigned, until the last number (if there are exactly ten numbers), and then the EOF causes scanf() to return. Note that OP could also have signalled EOF from the keyboard after each input. Or, OP could enter all numbers on one line, with an extra number to signal end of input:
1 2 3 4 5 6 7 8 9 10 11
The solution is simply to remove the \n from the end of the scanf() template string. Whitespace characters at the end of such a template string are tricky, and almost never what is actually desired.
Just remove the \n from the scanf format string:
scanf("%d", &number);
"\n" waits for non-white-space. Instead use scanf("%d", &number) #Michael Walz and check its return value.
Let us break down scanf("%d\n", &number);
"%d" Scan for numeric text. This specifier will allow leading white-space. Once some non-numeric character is found, put that character back into stdin. If valid numeric text for an integer was found, convert to an int and save to number.
"\n" Scan and discard 0 or more white-spaces such as '\n', ' ', and others. Continue scanning until non-white-space is encountered, put that character back into stdin.
Now return the number of fields scanned (1).
Notice step 2. User had to type in data after the Return or '\n' to get scanf() to return.
The issue is with the buffering of stdout which doesn't force a flush unless a \n is in the output or fflush is called specifically

Can't figure out why getchar() is picking up newline for first occurence in C

I am taking a training course on "C" and running into a problem. It's hard to explain so I'll post the code. This is training syntax, so don't ask me why it's done the way it is. When both of these segment blocks are run in a main(), the second block does not behave as if it exists alone in the main(). I have tried several IDE thinking it might be a bug.
/* First Segment Block */
int c;
printf("Type a letter: ");
c = getchar();
printf("You typed '%c'\n",c);
/* OR - outputs the same, to demonstrate putchar */
printf("You typed '");
putchar(c);
printf("'\n\n");
/* Second Segment Block */
int a,b,d;
printf("Type two letters: ");
a = getchar();
b = getchar();
d = getchar();
printf("You typed '");
putchar(a);
printf("' and '");
putchar(b);
printf("' and '");
putchar(d);
printf("'\n");
In the second segment block, I added a 3rd variable to test my theory. When you type the requested 2 letters, the first getchar() picks up a new line and the second getchar() picks up the first letter. The 3rd getchar() picks up the second letter. If you comment out the entire first segment block, then it behaves correctly, picking up the first letter by the first getchar() and the second letter by the second getchar(), displaying the output as expected.
Here is the output when both run together.
Type a letter: k
You typed (k)
You typed 'k'
Type two letters: lk
You typed '
' and 'l' and 'k'
RUN SUCCESSFUL (total time: 9s)
When they are run individually, the output is below.
First Segment Output.
Type a letter: k
You typed (k)
You typed 'k'
RUN SUCCESSFUL (total time: 5s)
Second Segment Output.
Type two letters: rf
You typed 'r' and 'f' and '
'
RUN SUCCESSFUL (total time: 5s)
The 3rd getchar() is a newline and that is expected.
Can anyone explain why when both segments are run in the same main(), the behavior is different from when run seperate?
Thank you in advance,
Daniel N. (C language beginner)
On the first prompt, you type something like aEnter, so your input stream contains the characters 'a', '\n'. The first getchar call reads the a and leaves the newline in the input stream.
In response to the second prompt, you type bcEnter, so your input stream now contains '\n', 'b', 'c', '\n'.
You can probably figure out what happens from here - the next getchar call reads that newline character from the input stream.
There are a couple of ways to deal with this. One is to test your input, and try again if it's a newline:
do
{
a = getchar();
} while ( a == '\n' ); // or while( isspace( a )), if you want to reject
// any whitespace character.
Another is to not use getchar; instead, use scanf with the %c conversion specifier and a blank space in the format string:
scanf( " %c", &c ); // you will need to change the types of your
... // variables from int to char for this.
scanf( " %c", &a );
scanf( " %c", &b );
scanf( " %c", &c );
The leading space in the format string tells scanf to ignore any leading whitespace, so you won't pick up the newline character.
Both 'space' and '\n (new line)' are characters as well. So, getchar() is getting every character you enter. For example, if you press 'a (enter) b', it will read a='a',b='\n',d='b'.
This is how input streams are.
when the first segment is running and to enter the first char, you press enter. It is basically taken as character itself in the input stream. so if you enter 'a' and press enter, its like 'a\n' in input stream.
Now with getchar, you read one char from input stream to your variable which is 'a' here. On giving you 'a', input stream is left with '\n' still.
next time you do getchar, you will get '\n' as char. even if you enter new chars, it will always be first in stream ie, '\nabc....'
'\n' it just to explain new line character.
This happens because your terminal is line buffered, so it's waiting for you to press Enter before passing any of the characters you've typed to your program. When you do press Enter, your program will receive any characters that you've typed, including the newline character (\n) produced by the Enter key.
The actual sequence of events looks like this:
Your program prints the first prompt.
Your program calls getchar(), which tries to read a character from the standard input stream. Since the stream is currently empty, it pauses your program until some input arrives.
You see the prompt and press a key (say, X). Since your terminal is line buffered, your program doesn't see it yet, and so remains paused.
You press Enter. Your terminal now send the characters you typed (X and \n) to your program.
getchar() returns the first character available to your program on the standard input, i.e. X.
Your code now prints the second prompt, and calls getchar() again. getchar() sees that there's another character (\n) still waiting on the standard input, and returns it immediately.

Break out of loop (do/while) by pressing a button [duplicate]

This question already has answers here:
What does space in scanf mean? [duplicate]
(6 answers)
Closed 7 years ago.
The following code gives the bizarre o/p as soon as I compile it.
main() {
char name[3];
float price[3];
int pages[3], i;
printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
scanf ("%c %f %d", &name[i], &price[i], &pages[i] );
printf ( "\nAnd this is what you entered\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( "%c %f %d\n", name[i], price[i], pages[i] );
}
But if we give the space in the scanf statement before %c, it gives proper o/p.
Can anyone please explain me why is it so?
Update:-
If I am providing the input like this-
F
123.45
56
J
134
67
K
145
98
then my question is why not we are giving space before %f and space before %d? Why we need to give space before %c only?
Adding the space to the format string enables scanf to consume the newline character from the input that happens everytime you press return. Without the space, name[i] will receive the char '\n', and the real char is left to be misinterpreted by %f.
So, say your input is
a 1.0 2
b 3.0 4
c 5.0 6
The program sees it more like this:
a 1.0 2\nb 3.0 4\nc 5.0 6\n\377
That is, the line-breaks are actual characters in the file (and the \377 here indicates "end of file").
The first scanf will appear to work fine, consuming a char, a float, and an integer. But it leaves the input like this:
\nb 3.0 4\nc 5.0 6\n\377
So the second scanf will read the '\n' as its %c, unless you get rid of it first.
Adding the space to the format string instructs scanf to discard any whitespace characters (any of space ' ', tab '\t', or newline '\n').
A directive is one of the following:
A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
...
from http://linux.die.net/man/3/scanf
This sort of problem arises whenever you use scanf with %c in a loop. Because, assuming free-form input, newlines can happen anywhere. So, it's common to try to avoid the whole issue by using a two-tiered approach. You read lines of input into a buffer (using fgets); chop-off the silly newline characters; then use sscanf instead of scanf to read from the buffer (string) instead of straight from the file.
Incorrect input using %c
Consider the following snippet of code:
int main( ){
int x;
char y;
scanf("%d", &x);
scanf("%c", &y);
printf("%d %c", x, y);
}
Behavior: When you run the above program, the first scanf is called
which will wait for you to enter a decimal number. Say you enter
12(That’s ASCII ‘1’ and ASCII ‘2’). Then you hit the "enter" key to
signal the end of the input. Next the program will execute the second
scanf, except you will notice that the program doesn't wait for you to
input a character and instead goes right ahead to output 12 followed
by a ‘\n’.
Explanation:Why does that happen? Let’s look at the behavior of the
program step-bystep.
Initially there is nothing in the buffer. When the first scanf() is called, it has nothing
to read, and so it waits. It keeps waiting until you type 1,2, then "enter". Now what's in
the buffer are the character 1, the character 2, and the character ‘\n’. Remember that ‘\n’
signifies the end of input, once all fields have been entered, but it is also stored in the
buffer as an ASCII character. At this point scanf will read the largest decimal input from
the buffer and convert that to an integer. In this example, it finds the string "12" and
converts it to the decimal value twelve and puts it in x. Then scanf returns control back to
the main function and returns the value 1, for being able to convert one entry
successfully. In our example, we do not catch the return value of the scanf in a variable.
For robust code, it is important to check the return value of scanf( ) to make sure that the
user inputted the correct data.
What is now left in the buffer is ‘\n’. Next, the second scanf is
called and it's expecting a character. Since the buffer already has
the ‘\n’ character in it, scanf sees that, takes it from the buffer,
and puts it in y. That's why when you execute the printf afterwards,
12 and “enter” are printed to the screen.
Solution: Moral of the story is, enter is a character like any other,
and is inputted to the buffer, and consumed from the buffer by %c just
like any other ASCII character. To fix this, try using this code
instead:
int main( ){
int x;
char y;
scanf("%d", &x);
scanf("\n%c", &y); /* note the \n !! */
printf("%d %c", x, y);
}
**
How does this fix the problem?
** So you again type ‘1’,’2’,’\n’. The first scanf reads "1" and "2", converts that to the decimal twelve and leaves the ‘\n’ in the buffer.
The next scanf now expects a ‘\n’ at the beginning of the next input.
It finds the ‘\n’ in the buffer, reads it, and discards it. Now the
buffer is empty and scanf is waiting on the user to input a character.
Say the user inputs ‘c’, followed by the enter key. ‘c’ is now
assigned to y, NOT "enter". Therefore printf will output "12 c" to the
screen. NOTE: there is again a ‘\n’ sitting in the queue now. So if
you need to do another scanf for a single character, you will have to
"consume" that '\n' before taking another character in from the user.
This is not an issue for any other format specifier, as they all ignore white spaces before
the input.

Why we need to put space before %c? [duplicate]

This question already has answers here:
What does space in scanf mean? [duplicate]
(6 answers)
Closed 7 years ago.
The following code gives the bizarre o/p as soon as I compile it.
main() {
char name[3];
float price[3];
int pages[3], i;
printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
scanf ("%c %f %d", &name[i], &price[i], &pages[i] );
printf ( "\nAnd this is what you entered\n" ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( "%c %f %d\n", name[i], price[i], pages[i] );
}
But if we give the space in the scanf statement before %c, it gives proper o/p.
Can anyone please explain me why is it so?
Update:-
If I am providing the input like this-
F
123.45
56
J
134
67
K
145
98
then my question is why not we are giving space before %f and space before %d? Why we need to give space before %c only?
Adding the space to the format string enables scanf to consume the newline character from the input that happens everytime you press return. Without the space, name[i] will receive the char '\n', and the real char is left to be misinterpreted by %f.
So, say your input is
a 1.0 2
b 3.0 4
c 5.0 6
The program sees it more like this:
a 1.0 2\nb 3.0 4\nc 5.0 6\n\377
That is, the line-breaks are actual characters in the file (and the \377 here indicates "end of file").
The first scanf will appear to work fine, consuming a char, a float, and an integer. But it leaves the input like this:
\nb 3.0 4\nc 5.0 6\n\377
So the second scanf will read the '\n' as its %c, unless you get rid of it first.
Adding the space to the format string instructs scanf to discard any whitespace characters (any of space ' ', tab '\t', or newline '\n').
A directive is one of the following:
A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
...
from http://linux.die.net/man/3/scanf
This sort of problem arises whenever you use scanf with %c in a loop. Because, assuming free-form input, newlines can happen anywhere. So, it's common to try to avoid the whole issue by using a two-tiered approach. You read lines of input into a buffer (using fgets); chop-off the silly newline characters; then use sscanf instead of scanf to read from the buffer (string) instead of straight from the file.
Incorrect input using %c
Consider the following snippet of code:
int main( ){
int x;
char y;
scanf("%d", &x);
scanf("%c", &y);
printf("%d %c", x, y);
}
Behavior: When you run the above program, the first scanf is called
which will wait for you to enter a decimal number. Say you enter
12(That’s ASCII ‘1’ and ASCII ‘2’). Then you hit the "enter" key to
signal the end of the input. Next the program will execute the second
scanf, except you will notice that the program doesn't wait for you to
input a character and instead goes right ahead to output 12 followed
by a ‘\n’.
Explanation:Why does that happen? Let’s look at the behavior of the
program step-bystep.
Initially there is nothing in the buffer. When the first scanf() is called, it has nothing
to read, and so it waits. It keeps waiting until you type 1,2, then "enter". Now what's in
the buffer are the character 1, the character 2, and the character ‘\n’. Remember that ‘\n’
signifies the end of input, once all fields have been entered, but it is also stored in the
buffer as an ASCII character. At this point scanf will read the largest decimal input from
the buffer and convert that to an integer. In this example, it finds the string "12" and
converts it to the decimal value twelve and puts it in x. Then scanf returns control back to
the main function and returns the value 1, for being able to convert one entry
successfully. In our example, we do not catch the return value of the scanf in a variable.
For robust code, it is important to check the return value of scanf( ) to make sure that the
user inputted the correct data.
What is now left in the buffer is ‘\n’. Next, the second scanf is
called and it's expecting a character. Since the buffer already has
the ‘\n’ character in it, scanf sees that, takes it from the buffer,
and puts it in y. That's why when you execute the printf afterwards,
12 and “enter” are printed to the screen.
Solution: Moral of the story is, enter is a character like any other,
and is inputted to the buffer, and consumed from the buffer by %c just
like any other ASCII character. To fix this, try using this code
instead:
int main( ){
int x;
char y;
scanf("%d", &x);
scanf("\n%c", &y); /* note the \n !! */
printf("%d %c", x, y);
}
**
How does this fix the problem?
** So you again type ‘1’,’2’,’\n’. The first scanf reads "1" and "2", converts that to the decimal twelve and leaves the ‘\n’ in the buffer.
The next scanf now expects a ‘\n’ at the beginning of the next input.
It finds the ‘\n’ in the buffer, reads it, and discards it. Now the
buffer is empty and scanf is waiting on the user to input a character.
Say the user inputs ‘c’, followed by the enter key. ‘c’ is now
assigned to y, NOT "enter". Therefore printf will output "12 c" to the
screen. NOTE: there is again a ‘\n’ sitting in the queue now. So if
you need to do another scanf for a single character, you will have to
"consume" that '\n' before taking another character in from the user.
This is not an issue for any other format specifier, as they all ignore white spaces before
the input.

Resources