c语言 在输入和输出中的作用,谈谈C语言中的输入和输出

要使用C语言中的输入和输出,第一步要引入标准的库函数

5c47d4fc24af75e7ffcbd944d0d1b64a.png

1.getc&putc函数

int getc( File * stream)

int putc(int char, File * stream)

getc函数通过接收一个流对象,将流中的一个字符读入,而putc将传入的字符输送到对应的流中,流对象既可以是标准输入输出流也可以是用户自己进行的文件操作。

如果是标准输入输出流对象的操作,那么这两个方法等价与getchar(void)和putchar(void) which means:

int character=getc(stdin)其效果等价与int character=getchar();

putc(character,stdout)=putchar(character);

void testChar(){

int character=0;

printf("请输入一个字符:");

character=getc(stdin);

int character2=0;

printf("请输入另外一个字符:\n");

character2=getchar();

printf("我是字符1:");

putc(character,stdout);

printf("\n");

printf("我是字符2:");

putchar(character2);

printf("h");

}

daf8c5ad7e235bf1a2e77e0164c2584b.png

当我们输入第一个字符x并且激动的按下回车的时候却发现,我们没有办法再输入第二个字符了!其实按下回车的这个动作也被stdin收录在缓冲区中了,也就是相当于x\n,所以最后我们打印出来的h是在新的一行出现的,而不是紧跟在字符2:之后。\n是不可见字符但是却占据一个字符的。

2.gets&puts函数

char *gets(char *str);

int puts(const char *str);

在C语言中没有定义字符串,在程序中要想表示字符串通常使用字符数组的形式 char str[size] ;而gets和puts函数是从stdin和stdout进行字符串的读入和写出的。这里要注意的是gets()中输入的长度不能够超过字符数组的size否则程序会报错

c8f533e6160cc7c8fb09c67066ca26ee.png

调用fgets函数的时候需要注意,fgets函数只能读取buffer容量的size-1个字符,fgets函数会默认将最后的一个字符填充为\0,并且将str内容进行返回。

1258e4f2c96d26091f873257138983bd.png

3.fgetc&fputc&fgets()&fputs()

其实这几个函数的作用和上面函数的差不多,只不过传递的参数不同

int fgetc(FILE *stream);

char *fgets(char *str, int n, FILE *stream);

int fputc(int char, FILE *stream);

int fputs(const char *str, FILE *stream);

int ungetc(int char, FILE *stream); //这个方法是将stream中的指针位置后退一步,也就是恢复一个字符。

void getcharMethod(){

int character=0;

int number=0;

char str[10];

char str2[10];

printf("you need to input an character:");

character=getchar();

printf("you need to input an interger too:");

scanf("%d",&number);

printf("please input another series of string:");

fgets(str2,10,stdin);

printf("you need to input a series of string:");

gets(str);//gets function does not work on linx OS so we need to use the fgets

printf("the character you input is %c\n",character);

// %c %f %s %d represents different types

printf("another way to ouput this charcter:");

putchar(character);

printf("\n");

puts(str);//puts function does not work on Linux OS

fputs(str2,stdout);

}

f56e4b6841ad52318727bed42b1addfb.png

stdin首先读入一个c\n9.getchar处理了第一个c,scanf又读取了后面的9此时的stdin中尚且还有一个换行字符,所以str2最后输出的也是空行。由于str2的容量大于1,所以str可以完整的读取abcdef并且输出。

我们来调整一下程序顺序

void getcharMethod(){

int character=0;

int number=0;

char str[10];

char str2[10];

printf("you need to input a series of string:");

gets(str);//gets function does not work on linx OS so we need to use the fgets

printf("please input another series of string:");

fgets(str2,10,stdin);

printf("you need to input an character:");

character=getchar();

printf("you need to input an interger too:");

scanf("%d",&number);

printf("the character you input is %c\n",character);

// %c %f %s %d represents different types

printf("another way to ouput this charcter:");

putchar(character);

printf("\n");

puts(str);//puts function does not work on Linux OS

fputs(str2,stdout);

}

当str2=1234567890,str=123456789的时候我们又发现了无法输入的问题

63f19801f67f245bc84060c713a3e57a.png

str2句末的时候塞了一个回车给str,而str又将后面的回车塞给了charcter.(O(≧口≦)O)!

所以如果str的可见字符长度为8的话,我们就可以继续进行输入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值