22

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);
}
zb226
  • 9,586
  • 6
  • 49
  • 79
gal
  • 419
  • 1
  • 4
  • 10

3 Answers3

53

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

Kenly
  • 24,317
  • 7
  • 44
  • 60
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 8
    C systems are **required** to flush output when the program ends. – Barmar Aug 27 '16 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? – David Ranieri Aug 27 '16 at 12:01
  • @AlterMann Running off the end of the function causes it to return to its caller. – Barmar Aug 27 '16 at 12:02
  • @Barmar, yes, `return` is optional, very strange! – David Ranieri Aug 27 '16 at 12:02
  • I got that from the question **you** linked to in a comment above. – Barmar Aug 27 '16 at 12:03
  • 4
    There's nothing wrong with the code, I find this answer very misleading... – Karoly Horvath Aug 27 '16 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 Jun 15 '20 at 02: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. – Jonathan Leffler Dec 05 '20 at 01:49
  • There's nothing misleading about this answer. The user had a simple problem and the answer provides a simple way to solve the problem. The explanation is also very helpful and relevant. The code is defective in that the user doesn't get the output they were looking for. – FontFamily Dec 06 '20 at 18:02
  • @FontFamily But... they do. They must. As has already been explained. – Asteroids With Wings Dec 08 '20 at 12:58
2

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
}
Garo
  • 1,339
  • 12
  • 21
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.