printf function in c doesn\'t always print the output on screen. For example if you forget to put \\n at the end of string you are printfing you sometimes don\'t get the o/p
its not that printf
won't always print, its that it isn't guaranteed to print immediately. This means that if you are using it for debugging purposes, then you can't guarantee that it will happen exactly when it does in the code. If you want to make sure that it does print exactly when you said it call
fflush(stdout)
.
Note: You typically don't want to use fflush(stdout)
unless you are debugging, its really resource intensive and if you care about speed performance at all it has the potential to slow you down.
I used
puts(largeString);
because in my particular case, printf() just stopped printing halfway through. The entire string was there, it just didn't print.
fflush(stdout) didn't fix it either, another printf() on the next line printed just fine.
As user1214634 said stdout is buffered and only prints to the screen under certain conditions. If you want to force it to print you can call fflush(stdout)
Standard out is a buffered stream, it is not guaranteed to flush unless a newline is put in, the stream is closed, or the program exits normally. If the program exits abnormally, it is possible for the stream to not flush. Standard out is line buffered, which is why a newline will flush it. There are buffers that will not flush with a newline.
There is another special case I just encountered:
My variables are:
line="-24 hours"
line2="24 hours"
and try
printf $line
printf $line2
Neither will work. The second one drops the word "hours" and the first one mistakes -24 as a flag.
Therefore, whenever I use printf I will remove all the dangerous characters if possible, by using
sed -r "s/[/\ #;&~]/_/g"
I wish printf's codes can be improved by the developer.
Take care