Running a program on Unix vs on Windows

I am compiling a simple program written in C, and I use Eclipse as an IDE, both on Windows 7 and on my MacBook Pro. A very simple program that my friend wrote and asked me to help him:

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); 

Therefore, when I run this on my mac, each line prints, and when I run into scanf (), I can enter and continue as expected. On Windows, I have to enter everything and then print all the lines. I'm not sure why this is happening ... what is the difference between Windows and Mac here?

Mac:

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

Window:

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

Thank you Christo

+4
source share
7 answers

As mentioned by this thread on Windows:

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

Also, due to error 27663 , the execution of printf() on the Eclipse console is not reset until the print buffer () is full.
This has various related errors for the Windows console: error 102043 and error 121454 .

+3
source

This is likely due to differences in buffer caching.

Try:

 fflush(stdout); 

before scanning. This will make the output appear on the screen when you need to see it.

+1
source

Windows and Mac create console output buffering in different ways. If you want it to appear immediately, you need to reset it by calling

 fflush(stdout); 

after printing. A.

+1
source

I assume that on Mac OS X "\ n" causes stdout to be reset, while this is not the case on Windows. Try adding the following code snippet after your print statements and before your scanf operations:

 fflush(stdout); 
+1
source

As Fedor said, this is most likely a problem with line endings.

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

On Mac OSX, the line ends with only "\ r", but "\ r \ n" also works because it includes "\ r".

On Unix / Linux, line endings are usually "\ n".

0
source

In addition to the answers about the need for fflush() - your code contains a buffer overflow. Scanf () in b writes 3 bytes - { 'B', '+', '\0' } - and there is not enough space in your array to store the NUL terminator. You need either a buffer with a width of 3 characters, or use something other than scanf(%s) with a to read two characters.

0
source

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

-1
source

Source: https://habr.com/ru/post/1309507/


All Articles