Why does this code with '1234' compile in C++?

后端 未结 2 655
我在风中等你
我在风中等你 2020-11-29 11:06

Why does this compile:

char ch = \'1234\'; //no error

But not anything more than 4 chars :

char ch = \'12345\'         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 11:50

    It's a multicharacter literal, and has a type of int.

    C++11 §2.13.2 Character literals

    A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. 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.

提交回复
热议问题