Why char delimiter? c-based

I wrote code in C # the other day, and it made me think. If a char literal can have only 1 char (or a separator character), then why do we need a closing delimiter?

For example, currently we need to write:

char character = 's'; 

And there are no circumstances when this might work (obviously):

 char character = 'sa'; 

Then why should we close ' , not imply? For instance:

 char character = 's; char anotherCharacter = '\'; 

Reading will be affected only because we are used to the current standard (not necessarily because this method is less readable).

+4
source share
4 answers

In C #, Java, and C ++, the answer is simple:

Since it was implemented in C.

Why was this implemented in C in this way?

Who knows? In the first C-parser / compiler, there may be some implementation attack that made this choice obvious; I'm sure K & R just didn't think about it. The only quote looks weird.

+2
source
  • This agreement
  • Symbols are not necessarily represented in one symbol, in this Unicode world
  • Escape / escape sequences can be longer than one character (e.g. '\ 0x1a')
+7
source

'abcd' is a multi-character literal; it is an int type, and its value is determined by the implementation. Closing ' necessary to mark the end of the literal.

+1
source

The existing C syntax defines a character constant as:

character is constant:

' c- char -sequence '

L' c-char -sequence '

Your question boils down to why this cannot be determined instead:

character is constant:

' c- char

L' c- char

Well, that could - in the sense that it would still be a consistent grammar of parsing, and you could still express single-character constants. What you couldn't do was express multi-character constants (like 'ab' ) - they are legal, but they have a value defined by the implementation.

I suspect that the true reason is simply aesthetics. For example, in theory there is also no reason why brackets are needed for conditionally expressing an if .

0
source

Source: https://habr.com/ru/post/1490607/


All Articles