I am using gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
I am writing a very simple script to take string as input and print the same with the some custom message. Fi
With your first call to scanf you allow the user to input an integer for the number of loops. They do so, and use a carriage return to signal the end of input. At this point scanf reads the integer, but leaves the end-of-line (\n) in the stream..
So when you call fgets, fgets gets from the stream until it reaches the first newline -- in you code, on the first loop, this is the very first character it encounters.
If you discard the newline before calling fgets, you should get your desired results. You can do this, for example by changing your first three lines to:
int T;
char newline;
scanf("%d%c",&T, &newline);
Although there are probably stylistically superior ways of doing this.
This is because your scanf()
call
scanf("%d",&T);
does not consume the new line character \n
accompanied by your input.
When you input T
, your input is 2Enter.
scanf()
consumes the 2
, sees \n
and stops consuming, leaving \n
in the input buffer.
When the fgets()
gets called, it sees the \n
in the buffer and treats this as the input.
To solve this, you have a few choices (in decreasing order of my subjective preference):
Use fgets()
to get the first input, then parse it using strtol()
to get T
.
Add an extra fgets()
after the scanf()
.
Add getchar()
to consume one extra character. This works if you are certain that exactly one \n
will be present after the input. So for example it won't work if you type 2SpaceEnter. Alternatively, you may use while(getchar() != '\n');
after the scanf()
to consume everything up to a new line, but this may cause problems if an empty line is a valid input to your later fgets()
calls.
If your implementation supports it, you may use fpurge(stdin)
or __fpurge(stdin)
.
And, very importantly, do not use fflush(stdin)
unless your implementation clearly defines its behavior. Otherwise it is undefined behavior. You may refer to this question for more details. Also, note that the fpurge()
and fflush()
methods may not work correctly if your input can be piped into the program.
You have a newline character in the input stream when you press "enter" after typing 2 for the first scanf. This is consumed by fgets() initially, hence it prints nothing (You replace the newline by \0
).
In the next line, your input is read and echoed after World
because you are printing it after it.
This line
scanf("%d",&T);
reads the input until the first non-numeral is found, which is newline
. Then fgets()
reads that newline for its first input.
I suggest using fgets()
to read the number too.
fgets(str,80,stdin);
sscanf(str, "%d", &T);