From Free Pascal wiki
Jump to: navigation, search

Deutsch (de) English (en) español (es) français (fr) italiano (it) русский (ru)

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:

 var ch: char;
      c: byte; 
   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 }

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:

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

A CHAR variable can also be used as a loop control variable:

   Loop: Char;
  for Loop := 'a' to 'c' do WriteLn(Loop);

See also

navigation bar: data types
simple data types

boolean byte cardinal char currency extended int64 integer longint pointer real shortint smallint word

complex data types

array class object record set string shortstring