C语言 警告:多字符字符常量 [-Wmultichar]|
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26086600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: multi-character character constant [-Wmultichar]|
提问by kri
/* Beginning 2.0 */
#include<stdio.h>
main()
{
printf(" %d signifies the %c of %f",9,'rise',17.0);
return 0;
}
Hello people
大家好
When I compile this, the compiler giving the following warning:
当我编译它时,编译器给出以下警告:
warning: multi-character character constant [-Wmultichar]|
And the output only prints einstead of rise.
并且输出只打印e而不是rise.
Are multiple characters not allowed in C?
C 中是否不允许使用多个字符?
How can I print the whole word (rise)?
如何打印整个单词 ( rise)?
Please help me out.
请帮帮我。
回答by Jerry Coffin
Try: printf(" %d signifies the %s of %f",9,"rise",17.0);.
试试:printf(" %d signifies the %s of %f",9,"rise",17.0);。
C distinguishes between a character (which is onecharacter) and a character string(which can contain an arbitrary number of characters). You use single quotes ('') to signify a character literal, but double quotes to signify a character stringliteral.
C 区分一个字符(一个字符)和一个字符串(可以包含任意数量的字符)。使用单引号 ( '') 表示字符文字,但使用双引号表示字符串文字。
Likewise, you specify %cto convert a single character, but %sto convert a string.
同样,您指定%c转换单个字符,但%s转换字符串。
回答by herohuyongtao
Use %sand ""for a character string:
将%s和""用于字符串:
printf(" %d signifies the %s of %f",9,"rise",17.0);
^^ ^ ^
For 'rise', this is valid ISO 9899:1999 C. It compiles without warning under gcc with -Wall, and a “multi-character character constant”warning with -pedantic.
对于'rise',这是有效的ISO 9899:1999 C。它在 gcc 下编译时没有警告-Wall,“multi-character character constant”使用-pedantic.
According to the standard (§6.4.4.4.10),
根据标准(§6.4.4.4.10),
The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.
The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.

