1.STDIN_FILENO定义于文件/usr/include/unistd.h
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
作为read,write,close等系统调用的文件描述符使用
#include <unistd.h>
#include <string.h>int main()
{char s[] = "hello\n";write(STDOUT_FILENO, s, strlen(s));return 0;
}
运行程序输出:
$ ./m
hello可以看到通过STDOUT_FILENO可以直接向标准输出输出内容
2.stdin声明于文件stdio.h
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
作为fopen,fprintf等io库函数的指针使用
#include <stdio.h>
#include <string.h>int main()
{char s[] = "hello, 88\n";fprintf(stdout, s, strlen(s));return 0;
}
运行程序输出:
$ ./m
hello, 88可以看到程序时可以直接使用stdout的,而不需要先通过fopen返回一个指向FILE的指针