在 C 程序中打印单个字符时,格式字符串中必须使用“%1s”吗?我可以使用类似“%c”的东西吗?
最佳答案
是的,%c
将打印一个字符:
printf("%c", 'h');
此外,putchar
/putc
也可以。来自“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).
编辑:
另请注意,如果您有一个字符串,要输出单个字符,您需要获取要输出的字符串中的字符。例如:
const char *h = "hello world";
printf("%c\n", h[4]); /* outputs an 'o' character */
关于c - C中输出单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/310032/