Why do programs sometimes "skip over" printfs? - c

I have the following code:
if (!strcmp(ent_child->d_name, "eeprom")){
printf("\tread_from_driver: found a match! ");//DEBUG
get_child_path(child_path, child_path, "eeprom");
printf("The path is: %s\n", child_path);//DEBUG
read_eeprom(child_path);
}
This causes a segfault at some point, (probably get_child_path), but the first printf never happens, even though when I fix the code to be this:
if (!strcmp(ent_child->d_name, "eeprom")){
while(1)
printf("\tread_from_driver: found a match! ");//DEBUG
get_child_path(child_path, child_path, "eeprom");
printf("The path is: %s\n", child_path);//DEBUG
read_eeprom(child_path);
}
It does happen. What's going on? This is definitely not the first time I observed this behavior.

stdout is line-buffered by default, which means that you only get updated output when you send a newline character, or when you explicitly call fflush(stdout).

Use \n in the end of each printf to make sure the output is flushed. Otherwise it's buffered and not immediately written.

Only stderr is not buffered ... stdout is buffered, and therefore you won't necessarily see the output until either the buffer is full, a newline character has been encountered, or you specifically flush the stream.
Therefore, if you're wanting to print debug messages, use stderr instead of stdout.

put \n in the end of the first printf, the segmentation fault warning eliminates the last output line. I cant really explain it, I just know that if you put a \n it is written

Related

Weird pointer behaviour when being passed through a function [duplicate]

I'm using Anjuta and gdb on Fedora 20 and created a C Makefile project. The code looks like this:
#include <stdio.h>
int main (void)
{
° printf ("1");
° printf ("2");
° printf ("3");
return (0);
}
° means I set a breakpoint at that position.
Now when I debug the code, there's no output while the current line is one of these printf-functions. Only when I exit main '123' appears in the terminal.
If I add \n to the second printf argument, then '12' appears as output when I move from breakpoint 2 to the 3rd one.
By default, stdout is line buffered when writing to a terminal, fully buffered when writing to any other type of stream. Since you're not printing any newlines, the output is being buffered. You can change the buffering mode with setbuf(), end each string with newline, or call fflush() when you want printing to take plac.
Add fflush(stdout) after each printf. Your output is small and remains in buffer until the progam exits.
This is because printf writes to stdout which happens to be buffered. For more details see here.

Why does printf output not appear right away when stepping through the code?

I'm using Anjuta and gdb on Fedora 20 and created a C Makefile project. The code looks like this:
#include <stdio.h>
int main (void)
{
° printf ("1");
° printf ("2");
° printf ("3");
return (0);
}
° means I set a breakpoint at that position.
Now when I debug the code, there's no output while the current line is one of these printf-functions. Only when I exit main '123' appears in the terminal.
If I add \n to the second printf argument, then '12' appears as output when I move from breakpoint 2 to the 3rd one.
By default, stdout is line buffered when writing to a terminal, fully buffered when writing to any other type of stream. Since you're not printing any newlines, the output is being buffered. You can change the buffering mode with setbuf(), end each string with newline, or call fflush() when you want printing to take plac.
Add fflush(stdout) after each printf. Your output is small and remains in buffer until the progam exits.
This is because printf writes to stdout which happens to be buffered. For more details see here.

Why does printf not work before infinite loop?

I am trying to make a small program that includes an infinite loop to wait for signal input from the user. I wanted to print out a message about the current working directory before beginning the infinite loop. The message works on its own, but when I put the infinite loop into the code the message does not print out (but the terminal does loop infinitely). The code is:
#include <stdio.h>
int MAX_PATH_LENGTH = 100;
main () {
char path[MAX_PATH_LENGTH];
getcwd(path, MAX_PATH_LENGTH);
printf("%s> ", path);
while(1) { }
}
If I take out while(1) { } I get the output:
ad#ubuntu:~/Documents$ ./a.out
/home/ad/Documents>
Why is this? Thank you!
When you call printf, the output doesn't get printed immediately; instead, it goes into a buffer somewhere behind the scenes. In order to actually get it to show up on the screen, you have to call fflush or something equivalent to flush the stream. This is done automatically for you whenever you print a newline character* and when the program terminates; it's that second case that causes the string to show up when you remove the infinite loop. But with the loop there, the program never ends, so the output never gets flushed to the screen, and you don't see anything.
*As I just discovered from reading the question itsmatt linked in a comment, the flush-on-newline only happens when the program is printing to a terminal, and not necessarily when it's printing to a file.
Because you don't have a new-line character at the end of your string. stdout is line-buffered by default, which means it won't flush to console until it encounters a new-line character ('\n'), or until you explicitly flush it with fflush().
Perhaps the output is not getting flushed. Try:
printf("%s> ", path);
fflush(stdout);
Because the stdout hasn't been flushed.
Call
fflush(stdout);
before your loop.
Because the output is not flushed.
Add
fflush(stdout);
before the while loop will solve the problem.

code sleeping in while loop expect the output

#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
The output of above on my machine is
hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
I had to kill the program to stop it.
Is it the correct and expected behavior.
Or it is wrong.This was an interview question hence I am posting here.
This is the expected output:
The program loops forever because of the while (1) loop.
stdout is line-buffered (by default), so it only flushes to the console when a new-line character ('\n') is printed. As you aren't printing any new-line characters, you never see any of the hello-out text.
However stderr is not line-buffered (by default), so it updates the console on every new fprintf() call.
stdout is buffered on most machines. You won't see any output from any fprintf calls to stdout unless you print a newline or call fflush().
So yes, it's expected behaviour, most of the time.
While everyone is right that you have an infinite loop, stderr is not buffered so you get it immediately, stdout is line buffered so it is deferred until you get a newline, they do not mention that stdout does not have infinite storage. I think the buffers are 1k or so by default (see setbuf). If you wait long enough you will get a very long burst of hello-out sequences. It is entirely possible that the last hello-out might be truncated part of the way through the string "hello-out".
[...]hello-outhello-outhellhello-errhello-err
^^^^

Why does printf() not print anything before sleep()?

I'm just learning C with Kernighan and Ritchie's book; I'm in the basics of the fourth chapter ("Functions and Program Structure"). The other day I became curious about the sleep() function, so tried to use it like this:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf(" I like cows.");
sleep(5);
return 0;
}
The problem is the output of the program, it looks like it does the sleep() first and then the printf(), in other words, it waits five seconds and then prints the string. So I thought, maybe the program gets to sleep() so fast that it doesn't let printf() have his work done like I want, that is print the string and then sleep.
How can I show the string and then put the program to sleep?
The compiler is GCC 3.3.5 (propolice) in OpenBSD 4.3.
printf() writes to stdout (the default output stream) which is usually line buffered. The buffer isn't flushed by the time sleep is called so nothing is displayed, when the program exits all streams are automatically flushed which is why it prints right before exiting. Printing a newline will usually cause the stream to be flushed, alternatively you could use the fflush function:
int main(void)
{
printf(" I like cows.\n");
sleep(5);
return 0;
}
or:
int main(void)
{
printf(" I like cows.");
fflush(stdout);
sleep(5);
return 0;
}
If you are printing to a stream that is not line buffered, as may be the case if stdout is redirected or you are writing to a file, simply printing a newline probably won't work. In such cases you should use fflush if you want the data written immediately.
Your problem is that printf (and anything else that uses the stdio library to write to stdout (standard output)) is buffered - line buffered if it goes to the console, and size buffered if it goes to a file. If you do a fflush(stdout); after the printf, it will do what you want. You could try just adding a newline ('\n') to your string, and that would do the right thing as long as you don't redirect standard output to a file.
I'm not 100% sure, but I think stderr isn't buffered, which can cause confusion because you might see output you made to stderr before output you previously made to stdout.
Buffering means that all the output is stored in a place (called buffer) and is output after a certain amount of data is present in it. This is done for efficiency reasons.
Some (most?) implementations clear the buffer after a newline when writing to the console, so you can also try
printf(" I like cows.\n");
instead of the call to fflush()
I implemented time encounter as following;
for (int i = 1; i <= 60; i++) {
printf("%02d", i);
fflush(stdout);
sleep(1);
printf("\b\b");
}

Resources