C中输出单个字符

发布于 2024-07-09 09:57:25 字数 59 浏览 7 评论 0原文

在 C 程序中打印单个字符时,我必须在格式字符串中使用“%1s”吗? 我可以使用“%c”之类的东西吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风苍溪 2024-07-16 09:57:25

是的,%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 */

yes, %c will print a single char:

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

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).

EDIT:

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 */
平安喜乐 2024-07-16 09:57:25

请注意 'c'"c" 之间的区别

'c' 是适合用 %c 格式化的字符

"c " 是一个 char* ,指向长度为 2 的内存块(带有空终止符)。

Be careful of difference between 'c' and "c"

'c' is a char suitable for formatting with %c

"c" is a char* pointing to a memory block with a length of 2 (with the null terminator).

青萝楚歌 2024-07-16 09:57:25

正如其他答案之一中提到的,您可以使用 putc(int c, FILE *stream)、putchar(int c) 或 fputc (int c, FILE *stream) 用于此目的。

需要注意的是,使用上述任何函数都比使用 printf 等任何格式解析函数要快得多。

使用 printf 就像使用机关枪发射一颗子弹。

As mentioned in one of the other answers, you can use putc(int c, FILE *stream), putchar(int c) or fputc(int c, FILE *stream) for this purpose.

What's important to note is that using any of the above functions is from some to signicantly faster than using any of the format-parsing functions like printf.

Using printf is like using a machine gun to fire one bullet.

少年亿悲伤 2024-07-16 09:57:25

输出单个字符的最简单方法是使用 putchar 函数。 毕竟,这是它的唯一目的,它不能做任何其他事情。 没有比这更简单的了。

The easiest way to output a single character is to simply use the putchar function. After all, that's it's sole purpose and it cannot do anything else. It cannot be simpler than that.

羅雙樹 2024-07-16 09:57:25
char variable = 'x';  // the variable is a char whose value is lowercase x

printf("<%c>", variable); // print it with angle brackets around the character
char variable = 'x';  // the variable is a char whose value is lowercase x

printf("<%c>", variable); // print it with angle brackets around the character
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文