Some C compilers permit multiple characters in a character constant. This means that writing 'yes' instead of "yes" may well go undetected. Source: C traps and pitfalls
Can anyone give an example of this where multiple characters are allowed in a character constant?
Also, there is a multi-character literal that contains more than one c-char. A single c-char literal has type char and a multi-character literal is conditionally-supported, has type int, and has an implementation-defined value.
Character constant can be written as one character within single quote such as '4' .
A character constant is one or more characters enclosed in single quotes, such as 'A' , '+' , or '\n' . In the mikroC PRO for PIC, single-character constants are of the unsigned int type.
A "character constant" is formed by enclosing a single character from the representable character set within single quotation marks (' '). Character constants are used to represent characters in the execution character set.
Multi-character constants are allowed in all contexts where single-character constants are allowed.
As for where they'd actually be used, I've seen code that uses multi-character constants to create legible unique values. For example, assuming that int is 4 bytes, 'ABCD' and 'EFGH' are likely to be distinct. (This isn't guaranteed by the language; the implementation must document the mapping, but it needn't be reasonable.) And assuming a reasonable mapping, you'll likely see "ABCD" or "EFGH" in the object code. Not the best idea in the world, but it can work if you don't care much about portability.
Incidentally, all conforming C compilers support multi-character constants (by definition; a compiler that doesn't support them is non-conforming).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With