assigning more than one character in char

后端 未结 2 501
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 14:29

Why this program gives output \'y\'

#include 

int main(void) {
    char ch=\'abcdefghijklmnopqrstuvwxy\';
    printf(\"%c\",ch);         


        
相关标签:
2条回答
  • 2020-12-09 15:03

    It's a multi-character literal.

    An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.

    Also from 6.4.4.4/10 in C11 specs

    An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

    So the line char ch = 'abcdefghijklmnopqrstuvwxy' on your system (assuming 4 byte int) possibly compiles to:

    char ch = 0x76777879;  // SOME int value (may be different, but documented in the compiler documents)
    

    ch will be assigned 'abcdef...y' which may be equivalent to (int)0x616263646566...79 in ascii encoding and overflows an integer. This is the reason why gcc generates the following warning:

    multicharlit.c: In function ‘main’:
    multicharlit.c:4:13: warning: character constant too long for its type [enabled by default]
    multicharlit.c:4:5: warning: overflow in implicit constant conversion [-Woverflow]

    It appears on your system, least significant 8 bits are used to assign to ch. Because your character literal is constant, this most possibly happens at compile time: (For example following happens when I compile with gcc)

    $ cat multicharlit.c
    #include <stdio.h>
    
    int main(void) {
        char ch='abcdefghijklmnopqrstuvwxy';
        printf("%c",ch);
        return 0;
    }
    
    $ gcc -O2 -fdump-tree-optimized multicharlit.c 
    $ cat multicharlit.c.143t.optimized 
    
    ;; Function main (main) (executed once)
    
    main ()
    {
    <bb 2>:
      __builtin_putchar (121);
      return 0;
    
    }
    

    Also stealing some goodness from unwind's comment

    Remember that the type of a single-quoted character constant is int, but you're assigning it to a char, so it has to be truncated to a single character.

    Type of 'a' for example is int in C. (Not to be confused with 'a' in C++ which is a char. On the other hand type of 'ab' is int in both C and C++.)

    Now when you assign this int type to a char type and value is more than that can be represented by a char, then some squeezing needs to be done to fit the result into less wider type char and the actual result is implementation-defined.

    0 讨论(0)
  • 2020-12-09 15:03

    If you intended to print out abcdefghijklmnopqrstuvwxy, then you should have stored it into a string variable instead of a char one (char ch[50] = char abcdefghijklmnopqrstuvwxy;).

    String variables can hold more than one character, where as a char variable is for holding one character.

    0 讨论(0)
提交回复
热议问题