Rules for Constructing Character Constants in C
In C, a character constant is a single character enclosed in single quotes (‘). Here are the rules for constructing character constants in C:
- A character constant must contain a single character.
- The character can be any printable ASCII character, an escape sequence, or a universal character.
- Escape sequences are used to represent special characters that cannot be typed directly. For example, the escape sequence ‘\n’ represents a newline character.
- Universal characters are used to represent characters in other character sets, such as Unicode. Universal characters are written using the ‘\u’ or ‘\U’ prefix followed by four or eight hexadecimal digits. For example, ‘\u20AC’ represents the Euro symbol (€).
- Character constants are of type ‘char’ and are stored as integers in the computer’s memory. The ASCII value of the character is stored in the memory.
Example:
char ch1 = 'A'; // ch1 will contain the ASCII value for 'A'
char ch2 = '\n'; // ch2 will contain the ASCII value for a newline character
char ch3 = '\u20AC'; // ch3 will contain the Unicode value for the Euro symbol
Leave a Comment