views:

97

answers:

7

I'm compiling a simple program written in C and I'm using Eclipse as an IDE, both in Windows 7 and on my MacBook Pro. Very simple program my friend wrote and asked me to help him with:

int a = 0;
char b[2];
printf("Input first class info:\n");
printf("Credit Hours: \n");
scanf("%d", &a);
printf("Letter Grade: ");
scanf("%s", b);

So when I run this on my mac, each line prints and when I encounter the scanf(), I can input and continue as expected. In Windows, I have to input everything and then it will print all the lines. I'm not sure why this is happening... what is the difference b/w Windows and Mac here?

Mac:

Input first class info:
Credit Hours: 4
Letter Grade: B+

Windows:

4
B+
Input first class info:
Credit Hours:
Letter Grade:

Thanks, Hristo

A: 

You want to use \r\n instead of \n.

Fyodor Soikin
No. Standard output is opened in text mode, which means that a plain `\n` is translated to whatever the native line ending is.
caf
@caf, but then again, Windows is flagrantly non-standard.... you can pretty much expect any sort of behavior on a Windows machine.
Michael Aaron Safyan
@Michael: It has nothing to do with Windows. Libraries are Eclipse's responsibility.
Fyodor Soikin
+1  A: 

It's likely due to buffer caching differences.

Try:

fflush(stdout);

before your scanfs. This will force the output to be flushed to the screen when you need to see it.

RC
+1  A: 

Windows and Mac are buffering console output differently. If you want it to appear immediately you need to flush it by calling

fflush(stdout);

after the printf.

Evgeny
+1  A: 

My guess is that on Mac OS X, the "\n" causes stdout to be flushed, while this is not so on Windows. Try adding the following piece of code after your print statements and before your scanf statements:

fflush(stdout);
Michael Aaron Safyan
A: 

Like Fyodor said, it's most likely a line-ending problem.

On Windows, the line ending is "\r\n" (carriage-return followed by line-feed).

On Mac OSX, the line ending is just "\r", but "\r\n" also works, because it includes the "\r".

On Unix/Linux the line-ending is usually just "\n".

Andy White
+3  A: 

As mentioned by this thread on Windows:

You need to fflush(stdout) after your call to printf().

Also, because of bug 27663, doing printf() to the Eclipse console doesn't flush until printf()'s buffer becomes full.
That has various associated bugs for Windows console: bug 102043 and bug 121454.

VonC
Thanks that fixed it. Do I need to flush after every printf() before the scanf()?
Hristo
@Hristo: You'll want to do this when and where you care to see all the data prior to the next line to be executed. I would advise not blindly putting fflush after every printf as it will degrade performance if overly used in a I/O intensive application.
RC
@RC: I agree. This was in the context of the thread mentioned.
VonC
A: 

In addition to the answers about the need for fflush() - your code contains a buffer overflow. The scanf() into b writes 3 bytes - { 'B', '+', '\0' } - and your array doesn't have enough room to store the NUL terminator. You either need a 3 character wide buffer, or to use something other than scanf(%s) with a for reading the 2 characters in.

godlygeek