How to send output to stderr?

After Rust 1.19 As of Rust 1.19, you can use the eprint and eprintln macros: fn main() { eprintln!(“This is going to standard error!, {}”, “awesome”); } This was originally proposed in RFC 1896. Before Rust 1.19 You can see the implementation of println! to dive into exactly how it works, but it was a …

Read more

Code for printf function in C [duplicate]

Here’s the GNU version of printf… you can see it passing in stdout to vfprintf: __printf (const char *format, …) { va_list arg; int done; va_start (arg, format); done = vfprintf (stdout, format, arg); va_end (arg); return done; } See here. Here’s a link to vfprintf… all the formatting ‘magic’ happens here. The only thing …

Read more

Rerouting stdin and stdout from C

Why use freopen()? The C89 specification has the answer in one of the endnotes for the section on <stdio.h>: 116. The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout), as those identifiers need not be modifiable lvalues to which the value returned …

Read more

GCC fatal error: stdio.h: No such file or directory

macOS I had this problem too (encountered through Macports compilers). Previous versions of Xcode would let you install command line tools through xcode/Preferences, but xcode5 doesn’t give a command line tools option in the GUI, that so I assumed it was automatically included now. Try running this command: xcode-select –install If you see an error …

Read more

stdlib and colored output in C

All modern terminal emulators use ANSI escape codes to show colours and other things. Don’t bother with libraries, the code is really simple. More info is here. Example in C: #include <stdio.h> #define ANSI_COLOR_RED “\x1b[31m” #define ANSI_COLOR_GREEN “\x1b[32m” #define ANSI_COLOR_YELLOW “\x1b[33m” #define ANSI_COLOR_BLUE “\x1b[34m” #define ANSI_COLOR_MAGENTA “\x1b[35m” #define ANSI_COLOR_CYAN “\x1b[36m” #define ANSI_COLOR_RESET “\x1b[0m” int main …

Read more