在C输出单个字符 [英] Output single character in C

查看:150
本文介绍了在C输出单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在C程序打印单个字符,我必须在格式字符串中使用%1?我可以使用类似%C?

When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"?

推荐答案

%C 将打印单个字符:

printf("%c", 'h');

此外,的putchar / putc将也可以工作。从人的putchar

also, putchar/putc will work too. From "man putchar":

#include <stdio.h>

int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);

* fputc() writes the character c, cast to an unsigned char, to stream.
* putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.
* putchar(c); is equivalent to putc(c,stdout).

编辑:

另外请注意,如果你有一个字符串,输出一个单个字符,你需要得到你想要输出字符串中的字符。例如:

Also note, that if you have a string, to output a single char, you need get the character in the string that you want to output. For example:

const char *h = "hello world";
printf("%c\n", h[4]); /* outputs an 'o' character */

这篇关于在C输出单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆