Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple characters in a character constant

Tags:

c

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?

like image 506
Chankey Pathak Avatar asked Aug 04 '11 16:08

Chankey Pathak


People also ask

Can a char be multiple characters?

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.

How many characters should be there to construct a character constant?

Character constant can be written as one character within single quote such as '4' .

What is the example of character constant?

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.

What is a character constant?

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.


1 Answers

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).

like image 170
Keith Thompson Avatar answered Oct 29 '22 10:10

Keith Thompson