putchar的用法
来源:网络收集 点击: 时间:2024-04-11通过putchar获取指定数字的图形:
比如putchar(97);会输出英语字母a,标明ASCII码97对应小写字母a 。

通过putchar获取指定字符的ASCII码:
asciiValue = putchar(a);
printf(ascii of a is :%d\r\n, asciiValue);

通过putchar实现字符串的打印函数:
#include stdio.h#include stdlib.h
/* run this program using the console pauser or add your own getch, system(pause) or input loop */void PrintString(char *str){while(*str){putchar(*str);str ++;}}
int main(int argc, char *argv) {char *str = Hello world !\r\n;PrintString(str);return 0;}

实际上putchar还可以用来输出汉字,但我们需要知道汉字的编码,具体百度查询,比如下面这个列出了汉字“中国”的GB2312编码、BIG5编码、GBK编码、GB18030编码、Unicode编码。

虽然putchar接收的是int型参数,不小于2字节,但是它实际是按照ASCII码处理的,即只具备一个字节的字符输出能力,所以对于占用2个字节的汉字需要分开输出:
#include stdio.h#include stdlib.h
/* run this program using the console pauser or add your own getch, system(pause) or input loop */
int main(int argc, char *argv) {
putchar(0xd6); //汉字中 GB2312编码高字节 putchar(0xd0); //汉字中 GB2312编码低字节 putchar(0xB9); //汉字国 GB2312编码高字节putchar(0xFA); //汉字国 GB2312编码低字节return 0;}

总结:
1、putchar()可以接收数字和字符形式的参数。
2、putchar()的返回值指示了字符输出的状态,成功则返回对应字符的ASCII码值,失败后返回非ASCII码值。
3、对一个字符串遍历,依次调用putchar()可以实现字符串的输出。
4、在知道汉字编码的情况下,可以分两步调用putchar(),实现汉字输出。

putchar()输出汉字需要知道其具体编码值,且需要拆分为两个字节,先输出高字节,再输出低字节。
PUTCHAR库函数C语言版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_488196.html