Sign in Join
Wiki copytemporary

Char

Deutsch · English · Español · Français · Italiano · Русский

A char stores a single character and is currently one byte, and AnsiChar is an alias for it. However, in the future, char may become the same as a WideChar. For now, byte and char are almost identical - one byte (8-bits) in size. However, a char can only be used as a character, or as part of a string type, and cannot be used in an arithmetic expression, while a byte can only be referred to as a numeric type.

For example:

pascal
 var ch: char;
      c: byte; 

 begin
   ch := 'A';  c := 65;  { are the same action, and are legal }
   ch := 65;   c := 'A'; { while they are internally the same values, 
                           direct assignment between Chars and Bytes is illegal }
 end.

The use of char or byte as a data type provides better documentation as to the purpose of the use of the particular variable. The char type can be coerced to byte by using the ord function. Byte type values can be coerced to char by using the chr function.

type chars functions follows the ASCII.

The above program corrected to legal use:

pascal
 var ch: char;
      c: byte; 

 begin
    ch := 'A';      c := 65;        { are the same action, and are legal }
    ch := chr(65);  c := ord('A');  { now legal }
    ch := Char(65); c := Byte('A'); { also legal and guaranteed to happen at compile time }
 end.

A FOR loop control variable can be any enumerable (capable of being put into one-to-one correspondence with the positive integers) which allows a CHAR variable to be used in that regard:

pascal
var
   Loop: Char;

begin
  for Loop := 'a' to 'c' do WriteLn(Loop);
end.

See also


From the Free Pascal wiki, page "Char", as it stood on 2023-10-17 after a change by FPC user, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.