jaska jaska - 9 months ago 33
C++ Question

Is stdout a natively usable token in C++?

Say I have a function like

void printToSomewhere(FILE* stream, char* msg){
fprintf(stream, "%s", msg);

If I want the stream to be
, do I have to declare that before in the calling function:

FILE* stdout;
printToSomewhere(stdout, "printing to stdout");

or can I call the function without having to define/include/etc stdout explicitly?

printToSomewhere(stdout, "printing to stdout");


As with every variable, you have to declare stdout before using it. The variable stdout is declared in the header file stdio.h (or cstdio in C++). By including stdio.h (or cstdio), stdout becomes visible.

On many platforms, you can also simply declare stdout as an extern variable:

extern FILE *stdout;

although doing so is discouraged, as the C standard requires stdout to be a macro and allows it to expand to something that is not even a variable. On most plaforms however, stdio.h defines this macro simply as

#define stdout stdout

but you should refrain from making this assumption in portable software.