Nik
Nik

Reputation: 375

Scan statements executed before print

I have this code.

#include <stdio.h>
int main(void)
{
     int dogs;
     printf("How many dogs do you have?\n");
     scanf("%d", &dogs);
     printf("So you have %d dog(s)!\n", dogs);
     return 0;
}

To my knowledge, when executing, the program will say 'How many dogs do you have' and I enter a number. Instead, I get a blank page, and I have to first enter a digit. Then I get

How many dogs do you have? So you have 3 dog(s)! (if I input 3)

I am using Eclipse + MinGW, and its my first time using C, so I'm not sure what I have/had to set up.

Upvotes: 2

Views: 2182

Answers (2)

Nik
Nik

Reputation: 375

I managed to solve this problem. The problem was that eclipse was first processing all the scan statements, and then processing all the print statements. This would be hard to solve if your program has more than one scan statement.

The fix is somewhat simple. Download the binary fix from the following link.

You then need to paste the starter.exe in the path where you have Eclipse installed.

In my case it was this eclipse\plugins\org.eclipse.cdt.core.win32.x86_64_5.2.0.201309180223\os\win32\x86_64

Yours may very a bit. Overwrite the existing starter.exe and it should work.

Upvotes: 1

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5721

printf() buffers output. In general (in UNIX) stdlib is smart enough to flush stdout before reading stdin but it might not be the case in MingW. Consider using fflush() or outputting to stderr.

Upvotes: 0

Related Questions