Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why doesn't it work on Linux? (C language)

+7 votes
asked May 22, 2023 by XY (200 points)

Hi Community!

// c0.c

#include <stdio.h>

void main (void)

{

   printf ("GNU Compiler Collection");

}

OUTPUT:

GNU Compiler Collection

However,

#include <stdio.h>

void main (void)

{

   printf ("GNU Compiler Collection");

   while (1);

}

OUTPUT:

INFINITE LOOP, BUT THE TEXT IS NOT DISPLAYED!

I know the solution: \n or fflush (stdout);

stdout is line buffered, this is Okay!

I'm interested in what prevents the text from being written out!

By the way it works perfectly on Windows, but not on Linux!

Thanks,

Regards,

XY

1 Answer

+1 vote
answered May 23, 2023 by Peter Minarik (88,110 points)

Hi XY,

I do not know the low level ins and outs of the C language, but here's my speculation:

Since you've already established that the printf() function is line buffered, the buffer needs to be emptied and pushed to the standard output. I suppose this happens automatically when the program terminates. That's why your first example has an output on Linux, but the second does not.

Why it works on Windows is probably because the implementation of printf() is different and perhaps there's no line buffering there, but everything immediately goes to the standard output.

An important thing: where the C standard does not specify the internal behaviour of a function, the implementer does whatever they feel happy with, hence different compilers produce different output.

DISCLAIMER: these are just speculations, but if I'd need to make a guess, this is what I'd go with.

commented May 24, 2023 by XY (200 points)
Hi Peter Minarik,
I'm XY.
Thanks for the answer!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...