Thread: Whats the difference between printf(), fprintf(stdout,), and fprintf(stderr,)?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Whats the difference between printf(), fprintf(stdout,), and fprintf(stderr,)?

    I've been checking out the code of a lot of my favorite programs written in C, and I just don't understand... Why do they sometimes output text using printf(, sometimes fprintf( to stdout and sometimes fprintf( to stderr??? What's the difference?

    Thanks.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It's a matter of pure control over which stream is actually used for some.
    When one needs to output an error message, fprintf on stderr is logical to use.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot.

    So it is really not much of a difference then. Although, I'm not so sure, but it seems that the program I got this from uses fprintf to stdin so that GUIs can parse the output easier. Again, I'm not sure if it's exactly the reason (I just saw some GUI parsing talk on comments) and I don't know why this is significant for GUIs.

    Edit: P.S.: and it uses fprintf to stderr for the help text.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    printf is the same thing as fprintf(stdout, it's just a matter of style.

    stdout goes to standard output, stderr standard error.

    both stdout and stderr are both redirected to the console (the screen) by default. But you can change that, by redirecting one or both to one or two files, or piping them to different application's input, or whatever. So there is some difference.

    search for file redirection.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well it is good practice to end a printf statement with a '\n'.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by slingerland3g View Post
    Well it is good practice to end a printf statement with a '\n'.
    For me it is sounds like "it is good practice to use bus for getting to your destination"

    I think the best trasportation type depends on the route... As well as adding \n in printf depends on the target...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    What makes stderr useful is that, were you, for example, to pipe output that's written to stdout to a file, you could write potential error messages to stderr, and they'd still be written to the console.

    Take the following example:

    Code:
    fprintf(stderr, "Writing number to file.\n");
    printf("Some number");
    Were this compiled, provided this piece of code was in a valid, compilable context, and executed with "program.exe > somefile.txt", you would get "Writing a number to a file." on the console while "Some number" would be written to somefile.txt.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vart View Post
    For me it is sounds like "it is good practice to use bus for getting to your destination"

    I think the best trasportation type depends on the route... As well as adding \n in printf depends on the target...
    I think what's more important is that if you're using \n in printf (and why wouldn't you? Most of the time you want output to be on separate lines), you put the \n at the end. I worked with a guy once who insisted on writing his debug as:
    Code:
    printf ("\nCalling foo");
    foo();
    printf ("\nReturned from foo");
    In this case if foo crashes you won't see the output of the first printf, unless something else flushes the buffer first.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed