15

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
  • 7,475
  • 4
  • 37
  • 64
gal
  • 339
  • 1
  • 2
  • 10
  • 2
    Add a "\n" to the string or the format. -->> `printf ("%s\n", a);` – wildplasser Aug 27 '16 at 11:33
  • 2
    why would it not print? – machine_1 Aug 27 '16 at 11:34
  • Or `fflush(stdout);` after `printf` – David Ranieri Aug 27 '16 at 11:35
  • 1
    which platform are you using to compile?? I have run it on `codeblocks` and `ideone`. it ran fine. – jbsu32 Aug 27 '16 at 11:36
  • 2
    You need to return an int from your function – samgak Aug 27 '16 at 11:37
  • 1
    @samgak, this is [optional](http://stackoverflow.com/questions/4138649/why-is-return-0-optional) – David Ranieri Aug 27 '16 at 11:39
  • sorry, I understood what is wrong. I have 2 different files with the same name, they are in the different directories. I use Linux and the clang compiler. – gal Aug 27 '16 at 11:40
  • 8
    Since you don't end the output with a newline, you may not be seeing it clearly, because it's mixed in with your shell prompt. – Barmar Aug 27 '16 at 11:59
  • Voting to reopen, the problem is reproducible and is not a typo but a common misunderstanding, and this Q/A is helpful to others with the same problem – M.M Jun 15 '20 at 02:16
  • 4
    Probably it works but you do not look at string before prompt. Something like `abcdeC:\Windows>` or `abcdeuser@host:~$`. – i486 Jun 15 '20 at 08:03
  • Does this answer your question? [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – Krishna Kanth Yenumula Dec 07 '20 at 04:15

3 Answers3

37

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
  • 16,220
  • 5
  • 36
  • 52
4386427
  • 33,845
  • 4
  • 32
  • 53
  • 5
    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
  • 3
    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
  • @4386427 Because it recently won you a "Lifeboat" badge, which [SO then blogged about](https://stackoverflow.blog/2020/12/04/podcast-292-goodbye-to-flash-well-see-you-in-rust/?cb=1) for no particular reason. – Asteroids With Wings Dec 08 '20 at 19:07
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.

-2

Doing a fflush() could be expensive if you also have other streams other than stdout open for write, like say a file outputting to disk. Running fflush() will flush all streams not just stdout. It would be better to be more specific and pass the stream to flush. So for printf() which outputs to stdout you'd use fflush(stdout). A alternative if you're looking to output errors would be to use stderr as that's unbuffered with fprintf().

Also you can use setvbuf to change an open streams buffer before writing to it. See man setvbuf

ConceptRat
  • 967
  • 6
  • 4