Caveat in character constant \'

Escape sequences are used to represent invisible or hard-to-represent characters. Some of them are used for avoiding ambiguities as listed below.

\" : Is only necessary in strings i.e. char const '"' (double quote in two single quotes) is valid.
\\ : Always reqd to distinguish \ from esc seq.
\? : To avoid trigraph sequence ??'. Char const '?' is valid.

All above escape sequences are justifiable coz possible ambiguities are obvios. But for escape sequence \' what is ambiguous if we use char const ''' (single quote in two single quotes) is not obvious. Following is the list characters tried to print through a C program and associated compiler errors.

char c = x;// x as below
printf("%c\n", c);

'' : Two single quotes without space in between.
error: empty character constant
Comment: This means empty character const is not permitted.

''' : Single quote char in two single quotes
error: missing terminating ' character
error: expected ; before printf
Comment: That is it marks 3rd single quote char as the start of new char const. Actually when earlier two single quotes doesn't represent a valid character (empty char) then probably three single quotes could have been allowed to represent single quote (') char const.

Also single quote char is perfectly valid in string const. So actually without suffereing from any ambiguity single quote char is pushed in escape sequence. This is a caveat which defies logical explanation.

I solicit readers' comments.

Comments

Popular posts from this blog

shriranga kamalakanta

Deva Gharache dnyat kunala

Hindu aikya ghosh