Zeina
Zeina

Reputation: 1603

How to define Empty Char in Delphi

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?

Upvotes: 15

Views: 21946

Answers (2)

David Heffernan
David Heffernan

Reputation: 612954

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.

Upvotes: 20

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

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'?

Upvotes: 29

Related Questions