Does C really need two single quotes (apostrophes) to delimit char literals instead of just one?
For string literals we do need to delimit the start and the end since strings vary in length, but it seems to me that we do know how long a char literal will be: either a single character (in the source), two characters if it is a regular character escape (prefix \0
), five characters if it is an octal literal (prefix \0[0-7]
), etc.
Keep in mind that I am looking for a technical answer, not a historical one. Does it make parsing simpler? Did it make parsing simpler on 70s hardware? Does it allow for better parsing error messages? Things like that.
(The same question could be asked for most C syntax inspired languages since most of them seem to use the same syntax to delimit char literals. I think the Jai programming language might be an exception since I seem to recall that it just uses a single question mark (at the beginning), but I’m not certain.)
Some examples:
'G'
'\0'
'\0723'
Would it work if we just used a single quote at the start of the token?
'G
'\0
'\0723
Could we in principle parse these tokens the same way without complicating the grammar?
We see that the null byte literal and the octal literal have the same prefix, but there might not be any ambiguity since there might not be any way that '\0
followed immediately by 723
might be anything else than a char literal (at least to my mind). And if there is an ambiguity then the null byte literal could become \z
instead.
Are the two single quotes needed in order to properly parse char literals?