Why are C character literals ints instead of chars?

后端 未结 12 1022
悲哀的现实
悲哀的现实 2020-11-22 02:24

In C++, sizeof(\'a\') == sizeof(char) == 1. This makes intuitive sense, since \'a\' is a character literal, and sizeof(char) == 1 as d

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 02:45

    I don't know the specific reasons why a character literal in C is of type int. But in C++, there is a good reason not to go that way. Consider this:

    void print(int);
    void print(char);
    
    print('a');
    

    You would expect that the call to print selects the second version taking a char. Having a character literal being an int would make that impossible. Note that in C++ literals having more than one character still have type int, although their value is implementation defined. So, 'ab' has type int, while 'a' has type char.

提交回复
热议问题