且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C输出单个字符

更新时间:2023-02-04 23:36:10

%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 */