ciosegmentation-faultunbuffered-output

Segmentation fault question


I have observed that sometimes in C programs, if we have a printf in code anywhere before a segmentation fault, it does not print. Why is this so?


Solution

  • It's because the output from printf() is buffered. You could add fflush(stdout); immediately after your printf and it would print.

    Also you could do this:

    fprintf(stderr, "error string");
    

    since stderr is not buffered.

    There's also a related question.