15

Just for curiosity,

Why in Delphi, if we defined an empty char by:

a:Char;
a:='';

we get an error: Incompatible types: 'Char' and 'string'

However, if we placed

a:='a';

it will be fine?

Is it necessary to define an empty char by: a:=#0?

2
  • 2
    In the .net world, you could have an "empty" Char because they have Nullable types (via their generics system). And in the database world, database fields can be Nullable, but all Delphi Ordinal types, including Integer, Char, Byte, and the rest, while they might have some sentinel (flag) or zero values (#0) for Char, for example, can not be "empty" or "null". Since strings and arrays are not Ordinal types the rules are different. A string is like a "dynamic array of characters". Commented Mar 8, 2012 at 14:13
  • 2
    You are confused about '' constant expression. It is of type string and not compatible with type char. In contrary 'a' is compatible. So, you have to define "empty char" convention. Commented Mar 8, 2012 at 14:29

2 Answers 2

29

A char is a single (that is, exactly one) character. So 'a', '∫', and '⌬' are all OK, but not 'ab' (a two-character string), 'Hello World!' (a twelve-character string), or '' (a zero-character string).

However, the NULL character (#0) is a character like any other.

In addition, the character datatype is implemented as a word (in modern versions of Delphi), that is, as two bytes. If all these values 0, 1, ..., 2^16 - 1 are used for real characters, how in the world would you represent your 'empty char'?

Sign up to request clarification or add additional context in comments.

15 Comments

u r right with the last edited phrase, I meant by empty char a null character, but i thought that when I place a:='', the compiler will take it as #0 by default, and didn't expect such error, so i wanted to know why is that. Thanks in advance!
@Zeina: Of course, the Delphi compiler could treat '' synonymously with #0, but I would find that confusing. In addition, does it really take that much longer to write #0 than ''?
@Marco: 'Legacy C systems'? The Windows API?
I have always disliked that Delphi uses single quotes for both Char and String literals. It may make coding easier, but it also causes confusion like this. I use C++ primarily, and it uses single quotes for characters and double quotes for strings. More explicit, and intuitive to me.
@JonathanElkins: No, that is not the case. '' is simply the empty string, and it is perfectly legal to use that combination, like in ShowMessage(''). But the empty string consists of zero characters, so it is not a character.
|
20

There is no such thing as an empty char. A char has to have a value. It is an ordinal type, a simple value type. Just as an integer, say, always has a value, so does a char.

The value #0 is not an empty char, it is the character with value 0, commonly known as NUL.

1 Comment

In Pascal thats ordinal type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.