【stdout,stdin】标准文件指针
stdio.h 文件把3个文件指针与3个C程序自动打开的标准文件进行了关联。
标准文件 |
文件指针 |
一般使用设备 |
标准输入 |
stdin |
键 盘 |
标准输出 |
stdout |
显示器 |
标注错误 |
stderr |
显示器 |
这些指针都是 FILE 指针类型,所以可以被用作标准I/O函数的参数。
#include<stdio.h>
int main(){
fprintf(stdout,"Hello World");
return 0;
}
运行结果:
#include<stdio.h>
#include<stdlib.h>
#if 1
int main(){
char s[20];
fscanf(stdin,"%s",s); //从键盘上获取一个字符串赋给s,以'\0'结尾代表结束
fprintf(stdout,s); //将s打印到屏幕
return 0;
}
运行结果:
#include<stdio.h>
#define MAXSIZE 50
int main(){
char line[MAXSIZE];
while(fgets(line,MAXSIZE,stdin) != NULL && line[0]!='\n'){ // stdin 从键盘获取的标准输入,以'\n'换行结尾为结束
fputs(line,stdout); // stdout 向屏幕输出的标准输出
}
/*
如果你在新的一行起始处键入回车键,
fgets()函数读入换行符然后把它放进数组line的第一个元素中
那么while循环将停止
*/
return 0;
}
运行结果:
不输入任何信息并回车
参考书籍:
《C Primer Plus (第五版)》Stephen Prata 云巅工作室译