loop problem

Here is my code ,
#include <stdio.h>
main(int argc,char *argv[])
{

int size=0;
int input[30];
printf("Enter the number of terms");
scanf("%d ",&size);
int i=0;
for( i=0;i<size;i++)
{
        printf("%d =",i);
        scanf("%d \n" ,&input[i]);

}
for (i=0;i<size;i++)
{
printf("%d \n",input[i]);
}
}
hen I run this the output is
Enter the number of terms3
5
0=6
1=7
2=8
5
6
7
The loop does not 8 inside its loop.instead it takes 5 into its loop.I want  to it to be
0=5
1=6
2=7
purtutuAsked:
Who is Participating?
 
gj62Connect With a Mentor Commented:
Remove the space in your scanf:

scanf("%d ",&size);

should be

scanf("%d",&size);
0
 
ecwCommented:
after printf, use fflush(stdout).  if writing to a tty, stdout is by default line buffered.  ie. it outputs after a newline.
0
 
gj62Commented:
ecw - he had a space in his scanf format control string - you don't need to flush stdout... unless you were going to do a gets or something similar after the scanf...
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
purtutuAuthor Commented:
Thanks a bunch .Its working now.
0
 
purtutuAuthor Commented:
Hi again,Could you kindly tell me what happens when i keep space in scanf()
0
 
gj62Commented:
As I understand it, scanf reads, but doesn't store, the whitespace character (defined as blank, tab *or* newline) if it is part of the format control string.  The expected space gets left there until your next entry of something other than a whitespace.

That's also why fflush wouldn't fix the problem either.
0
All Courses

From novice to tech pro — start learning today.