Reputation: 1449
#include<stdio.h>
void main(){
int num1,num2;
printf("\n Enter number 1 \t "); // Ask for input one. >>>>>>>> line 1.
scanf("%d ",&num1);
printf("\n Entered number is %d \n",num1);
printf("\n Enter number 2 \t "); // Ask for input Two. >>>>>>>>> line 2.
scanf("%d ",&num2);
printf("\n Entered number is %d \n",num2);
return;
}
I wish to know REASON.Please do provide it.
The code above accepts two inputs,first input is asked(By executing line 1) then user enter one number then terminal should ask to enter second input but instead it is taking other number(before executing line2 ) and then asking to enter second input(i.e after executing line 2).
In the End is is displaying the two input that are taken before executing line two but after executing line 1.
I am confused.I am interested to know reason. I am using GCC 4.8.2 on ubuntu 14.04 64 bit machine.
Upvotes: 0
Views: 126
Reputation: 153640
Let's take this apart slowly
#include <stdio.h>
// void main(){
int main(void) {
int num1, num2;
printf("\n Enter number 1 \t ");
scanf("%d ",&num1);
printf("\n Entered number is %d \n",num1);
...
Use a proper function signature for main()
- but that is not the main issue.
Code prints "\n Enter number 1 \t "
"%d"
directs scanf()
to scan for text convertible to an int
in 3 steps:
A: Scan for optional leading white-space like '\n'
, '\t'
, ' '
. Throw them away.
B: Scan for text representing an integer like "+1234567890". If any digits found, save the converted result to the address &num1
.
C: Scanning continues in step B until a char
that does not belong to the int
is found. That char
is "un-read" and returned to stdin
.
(This is where you get in trouble as suggested by @Chandru) " "
directs scanf()
to scan for any number (0 or more) white spaces including '\n'
, '\t'
, ' '
and others. Then they are thrown away - not saved.
B: Scanning continues!! until a non-white-space is found. That char
is "un-read" and returned to stdin
.
Lastly scanf()
return the value of 1
as that is how many field specifiers were converted. Code sadly did not check this value.
Recall stdin
is usually line buffered, which means input is not available to user IO until Enter is hit.
User enters 1 2 3 Enter and scanf()
scans the "123" and saves 123
to &num1
. Then it scans "\n" as that is a white-space in step 4. and it continues waiting for more white-space.
User enters 4 5 6 Enter and the first scanf()
which is not yet done, scans '4'
, sees it is not a white space and puts '4'
back in stdin
. scanf()
finally returns after 2 lines are entered. At this point "456\n" are waiting in stdio
for subsequent scanf()
.
The second scanf()
then perpetuates the issue.
Recommend use fgets()
only for all user input, at least until your are very skilled in C.
printf("\n Enter number 1 \t ");
char buf[100];
fgets(buf, sizeof buf, stdin);
if (sscanf(buf, "%d", &num1) != 1) Handle_BadInput();
else printf("\n Entered number is %d \n",num1);
Upvotes: 0
Reputation: 1334
Remove spaces between the scanf of access specifier.
scanf("%d ",&num1);
to
scanf("%d",&num1);
Because the scanf get the another value due to that spaces.
And kept in the buffer. After the memory has got it get assigned.
It is for all scanf function.
if I input like
Enter Number1 1
2
Entered number is 1
Enter number2 3
Entered number is 2.
Upvotes: 3
Reputation: 11
You have given a space in scanf for %d. If you remove that space after %d the program will run
Upvotes: 1
Reputation: 604
int main()
and in the end write return 0;
fflush(stdout);
to flush your buffer. After editing here is the final code
#include<stdio.h>
int main(){
int num1,num2;
printf("\n Enter number 1 \t "); // Ask for input one. >>>>>>>> line 1.
scanf("%d ",&num1);
printf("\n Entered number is %d \n",num1);
printf("\n Enter number 2 \t "); // Ask for input Two. >>>>>>>>> line 2.
fflush(stdout);
scanf("%d ",&num2);
printf("\n Entered number is %d \n",num2);
return 0;
}
Here is the Demo.
Upvotes: 2
Reputation: 60007
You need to put
fflush(stdout);
before the scanf
This will flush your buffer
(also a good idea to check the return value of scanf
)
Upvotes: 1