putchar的作用是把指定的字符写入到标准输出“stdout”中,其语法是“int putchar(int char)”,参数char表示要被写入的字符,该字符以其对应的int值进行传递。
putchar语法结构为 int putchar(int char) ,其功能是把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中,为c 库函数 ,包含在c 标准库 <stdio.h>中。其输出可以是一个字符,可以是介于0~127之间的一个十进制整型数(包含0和127),也可以是用char定义好的一个字符型变量。
putchar语法
(1)函数声明
int putchar(int char)(2)参数
char– 这是要被写入的字符。该字符以其对应的 int 值进行传递。
(3)功能
把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中。 [3]
(4)说明
该函数将指定的表达式的值所对应的字符输出到标准输出终端上。表达式可以是字符型或整型,它每次只能输出一个字符。例如:“putchar('#')”输出字符“#”。
应用格式
putchar函数的基本格式为:putchar(c)。
(1)当c为一个被单引号(英文状态下)引起来的字符时,输出该字符(注:该字符也可为转义字符);
(2)当c为一个介于0~127(包括0及127)之间的十进制整型数时,它会被视为对应字符的ascii代码,输出该ascii代码对应的字符;
(3)当c为一个事先用char定义好的字符型变量时,输出该变量所指向的字符。
注意事项
使用字符输入/输出函数时,必须在程序的前面加上头文件#include <stdio.h>或#include "stdio.h"。并且,该函数的变量及输出结果只能为一个字符。
函数返回值
该函数以无符号 char 强制转换为 int 的形式返回写入的字符。
(1)当输出正确的时候,返回输出字符转换为的unsigned int 值;
(2)当输出错误的时候,返回 eof(end of file)文件结束符
if(putchar(c)==eof){printf("output error:%m\\\\n");exit(0);}程序示例
示例1
#include <stdio.h>/* define some box-drawing characters */#define left_top 0xda#define right_top 0xbf#define horiz 0xc4#define vert 0xb3#define left_bot 0xc0#define right_bot 0xd9int main(void){char i, j;/* draw the top of the box */putchar(left_top);for(i=0; i<10; i ){putchar(horiz);putchar(right_top);putchar('\\\\n');}/* draw the middle */for(i=0; i<4; i )putchar(vert);for (j=0; j<10; j ){putchar(' ');putchar(vert);putchar('\\\\n');/* draw the bottom */putchar(left_bot);}for(i=0; i<10; i ){putchar(horiz);putchar(right_bot);putchar('\\\\n');return 0;}}示例2
#include <stdio.h>int main(){char a,b,c;a='t';b='m';c='d';putchar(a);putchar(b);putchar(c);putchar('\\\\n');putchar(a);putchar('\\\\n');putchar(b);putchar('\\\\n');putchar(c);putchar('\\\\n');return 0;}输出结果为:
tmdtmd推荐:《c语言教程》
域名无法绑定主机-虚拟主机/数据库问题云存储服务器的购买云服务器bcc和bae区别格式工厂将qlv转换mp4怎么做 格式工厂将qlv转换mp4的方法宋九九:我的服务器又被封了,说说服务器被封那些事提交错资料了-备案平台腾讯企业邮箱所需要填写的MX记录值美国云主机哪个好