c语言中的putchar()、getchar()用法解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zztingfeng/article/details/81839973

头文件  #include<stdio.h>

putchar()

1、函数声明

putchar()函数的声明:int putchar(int char)

2、函数描述

把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中。

3、参数

str :是要被写入的字符。该字符以其对应的 int 值进行传递。

4、函数返回值

该函数以无符号 char 强制转换为 int 的形式返回写入的字符,如果发生错误则返回 EOF。

5、例子

#include <stdio.h>

int main ()
{
   char ch;
   for(ch = 'A' ; ch <= 'Z' ; ch++) {
      putchar(ch);
   }
   return 0;
}

 

getchar()

1、函数声明

getchar()函数的声明:int getchar(void)

2、函数描述

从标准输入 stdin 获取一个字符(一个无符号字符)。这等同于 getc 带有 stdin 作为参数。

3、参数

NULL

4、函数返回值

该函数以无符号 char 强制转换为 int 的形式返回读取的字符,如果到达文件末尾或发生读错误,则返回 EOF。

5、例子

#include <stdio.h>
#include <string.h>

int main()
{
   printf("请输入字符:");
   c = getchar(); 
   printf("输入的字符:");
   putchar(c);

   return 0;
}

猜你喜欢

转载自blog.csdn.net/zztingfeng/article/details/81839973