24

I am learning to program in C. Could you explain why nothing is printed here?

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s", a);
}
0

3 Answers 3

55

On many systems printf is buffered, i.e. when you call printf the output is placed in a buffer instead of being printed immediately. The buffer will be flushed (aka the output printed) when you print a newline \n.

On all systems, your program will print despite the missing \n as the buffer is flushed when your program ends.

Typically you would still add the \n like:

printf ("%s\n", a);

An alternative way to get the output immediately is to call fflush to flush the buffer. From the man page:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.

Source: http://man7.org/linux/man-pages/man3/fflush.3.html

EDIT

As pointed out by @Barmar and quoted by @Alter Mann it is required that the buffer is flushed when the program ends.

Quote from @Alter Mann:

If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination.

See calling main() in main() in c

10
  • 8
    C systems are required to flush output when the program ends.
    – Barmar
    Commented Aug 27, 2016 at 11:57
  • 1
    @Barmar, good point: If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination. But in this case there is no call to exit() nor return, could this be what's causing the problem? Commented Aug 27, 2016 at 12:01
  • 4
    There's nothing wrong with the code, I find this answer very misleading... Commented Aug 27, 2016 at 12:49
  • 1
    There is also C11 7.21.2/2 "Whether the last line requires a terminating new-line character is implementation-defined"
    – M.M
    Commented Jun 15, 2020 at 2:19
  • 1
    @DavidRanieri: Note that C99 follows C++98 in that falling off the end of main() is equivalent to executing return 0;. I don't like that; I regard it as a misfeature. However, it is standard C for the whole of the current millennium, so the program shown has a valid exit status unless compiled for C90. Commented Dec 5, 2020 at 1:49
3

Strangely enough, it seems that nobody posted the adjusted code where the buffer is flushed yet...:

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s", a);
    fflush(stdout);
    //On some systems the line above will fail, in that case use: fflush(NULL);
}

Also note that this code probably doesn't do what you actually want to do.
What I assume you really want to do is:

#include <stdio.h>

int main (void)
{
    char a[]="abcde";
    printf ("%s\n", a);
   //The '\n' makes sure the next thing you print will be on the following line
}
0

Hopefully, I can make a couple of points about this without making it confusing. Printf is not the thing being buffered, it's stdio, so all similar functions will behave in the same way. To demonstrate the buffering, all you have to do is printf a lot of characters, usually more than 1024, and printf will print as you will have exceeded the maximum buffer length and it will automatically fflush. All the other points are, of course, also correct and valid.

Not the answer you're looking for? Browse other questions tagged or ask your own question.