0

Recently i mistakenly used

char ch1='10';
char ch2='a';
cout<<ch1*ch2<<endl;

and I received output as 4656 with a warning: warning: multi-character character constant [-Wmultichar] char c2 = '10'; ^~~~ Now i have no clue what is just happened. Can anyone help me understand?

  • BCPL programming language and B programming language supported multi-character constants. Some C compilers and C++ compilers support multi-character constants, as an extension. q.v. [character constant](https://en.cppreference.com/w/c/language/character_constant) under syntax (6) and under **Notes**. – Eljay Sep 25 '22 at 16:40
  • 1
    `'10'` is not a (valid) character*. Do you mean the character with _value_ 10 (line feed)? That is `char ch1=10;` Do you want the _string_ `"10"`? That is `char ch1[] = "10";` (*Multi-character constants exist, but are _compiler specific_ values.) – Dúthomhas Sep 25 '22 at 16:42
  • Does this give you an answer to your question, [C++ Char Initialization Allowed With Multiple Characters](https://stackoverflow.com/questions/41331212/c-char-initialization-allowed-with-multiple-characters?) – kesetovic Sep 25 '22 at 16:45
  • This could help you understand multi-character literals too, [What do single quotes do in C++ when used on multiple characters?](https://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters) – kesetovic Sep 25 '22 at 16:47
  • @Eljay -- to be clear, multi-character constants are part of the language, both C and C++; their **values** are implementation-defined. That note in your link is talking about their values, not their existence. – Pete Becker Sep 25 '22 at 18:51

0 Answers0