C中输出单个字符
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,
%c
将打印单个字符:此外,
putchar
/putc
也可以工作。 来自“man putchar”:编辑:
另请注意,如果您有一个字符串,要输出单个字符,您需要获取要输出的字符串中的字符。 例如:
yes,
%c
will print a single char:also,
putchar
/putc
will work too. From "man putchar":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:
请注意
'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).正如其他答案之一中提到的,您可以使用 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.
输出单个字符的最简单方法是使用 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.