c - Ambigious nature of scanf taking input before it is asked to enter from user? -
c - Ambigious nature of scanf taking input before it is asked to enter from user? -
#include<stdio.h> void main(){ int num1,num2; printf("\n come in number 1 \t "); // inquire input one. >>>>>>>> line 1. scanf("%d ",&num1); printf("\n entered number %d \n",num1); printf("\n come in number 2 \t "); // inquire input two. >>>>>>>>> line 2. scanf("%d ",&num2); printf("\n entered number %d \n",num2); return; }
i wish know reason.please provide it.
the code above accepts 2 inputs,first input asked(by executing line 1) user come in 1 number terminal should inquire come in sec input instead taking other number(before executing line2 ) , asking come in sec input(i.e after executing line 2).
in end is displaying 2 input taken before executing line two after executing line 1.
i confused.i interested know reason. using gcc 4.8.2 on ubuntu 14.04 64 bit machine.
remove spaces between scanf of access specifier.
scanf("%d ",&num1);
to
scanf("%d",&num1);
because scanf value due spaces.
and kept in buffer. after memory has got assigned.
it scanf function.
if input like
enter number1 1 2 entered number 1 come in number2 3 entered number 2.
c gcc printf scanf
Comments
Post a Comment