warning: multi-character character constant [-Wmul

2019-02-21 06:46发布

问题:

This question already has an answer here:

  • Multi-character constant warnings 4 answers
/* 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 e instead of rise.

Are multiple characters not allowed in C?

How can I print the whole word (rise)?

Please help me out.

回答1:

Try: printf(" %d signifies the %s of %f",9,"rise",17.0);.

C distinguishes between a character (which is one character) 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 string literal.

Likewise, you specify %c to convert a single character, but %s to convert a string.



回答2:

Use %s and "" for a character string:

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.

According to the standard (§6.4.4.4.10),

  • The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.


标签: c char