-2

I use the write syscall using the unistd.h header to send text to stdout. AFAIK, the numeric value of stdout is 1. So why can't I use stdout instead of 1 as the first argument of write?

#include <unistd.h> // write
#include <stdio.h> // stdout

int main(void)
{
    write(stdout, "This will be output to standard out\n", 36);
    return 0;
}

Google said: Standard output value stdout. Its value is 1.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

The first argument to write is a file descriptor, which is an integer. This is the native Unix I/O.

stdout here is an object that is intended for use with C library I/O routines. It is a pointer to an internal FILE object. It is not an integer.

stdout in other contexts means other things. We often use it to refer to the Unix concept of the 'standard output' file descriptor, which is indeed 1. But that doesn't mean that 'stdout' in an arbitrary programming language is identically equal to 1. Rather, it will be whatever mechanism the language runtime needs to handle the output.

J.Backus
  • 1,441
  • 1
  • 6
  • 6