gpt4 book ai didi

c - C中输出单个字符

转载 作者:行者123 更新时间:2023-11-30 16:39:11 24 4
gpt4 key购买 nike

在 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/47151817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com