Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why delimiter on char? c-based

Tags:

c++

c

c#

char

literals

I was writing some code in C# the other day and it got me thinking. If a char literal can only have 1 char (or a delimited character) then why do we need the closing delimiter?

For example, currently we need to write:

char character = 's';

And there is no circumstance where this can work (obviously):

char character = 'sa';

Then why do we have to put the closing ' rather than have it implied? Eg:

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

Readability would only be impacted because we are used to the current standard (not necessarily because this way is less readable).

like image 660
Sellorio Avatar asked Dec 26 '22 00:12

Sellorio


2 Answers

  1. It's a convention
  2. Characters are not necessarily represented in one character, in this unicode world
  3. Control / escape sequences can be longer than one character (e.g. '\0x1a' )
like image 110
Curt Avatar answered Jan 08 '23 03:01

Curt


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

Because that's the way it was implemented in C.

Why was it implemented in C that way?

Who knows? There may be some implementation quirk in the first C parser/compiler that made this choice obvious; I'm betting that K&R just didn't think about it. A single quote just looks weird.

like image 35
Eli Algranti Avatar answered Jan 08 '23 03:01

Eli Algranti