Why do programs sometimes "skip over" printfs?
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");
print开发者_JAVA百科f("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
精彩评论