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语言教程》

更多相关文章

  1. 带你了解C语言中的Sleep函数(附代码)
  2. putchar函数在C语言中是什么意思
  3. C语言中main函数可以在什么位置
  4. 在一个C语言程序中,main函数可以在任何地方出现么
  5. C语言中字符串连接函数是什么
  6. 成员函数可以重载吗?
  7. 如何使用c语言中的strlen()函数
  8. c语言的标识符只能由哪三种字符组成?
  9. strcat函数的作用是什么?

随机推荐

  1. android下的jni
  2. StevGuo系列文章翻译之Talking about And
  3. What Android Is
  4. 编译Irrlicht On Android(1)
  5. android系统学习笔记二
  6. android之蓝牙操作(二)
  7. Android 用MediaCodec实现视频硬解码
  8. Android四大组件之activity之间带数据跳
  9. Android中ListView多次调用getView
  10. 【Android开发】Toolbar与标题居中