Null character in Delphi (Caret notation: ^@)

I was assigned with a new programming task and I found the following constant declaration in the base code (Delphi 2007):

  s1 : PChar = ^@;

I was not sure about the meaning of such statement: the first ideas in my mind pointed me to think about some kind of pointer related syntax. I was induced to think so for the caret (^) and the at (@) symbols (operators?), which allow dereferencing a pointer and returning the address of a variable respectively.

Despite my hunch, the meaning of the above constant declaration was very different from my initial thoughts. Actually, it’s quite trivial…it just declares a constant, named s1, as type PChar and with the null character as constant value. Such declaration might be substituted with the following alternatives:

  s1 : PChar = '';

  s1 : PChar = #0;

The original construction is known as Caret notation. “In caret notation, the null character is ^@. Since ^A is character 1, then 0 must use the character before 'A' which is '@' in ASCII.”

The Caret notation for the Null character works well in both Ansi and Unicode versions of Delphi; meaning that you can use such syntax all the way from Delphi 1 through Delphi XE. Anyhow, the Caret notation is not well known by all programmers: one good example was myself :-)

So, if you are going to use it, make sure you append a small comment. This comment will allow further readers of your code to understand the syntax on the fly.