Difference between revisions of "Char"
(spelling correction) |
(Fix Char(49) typo.) |
||
(15 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{Char}} | {{Char}} | ||
− | A '''char''' | + | 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|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|string]] type, and cannot be used in an arithmetic expression, while a byte can only be referred to as a numeric type. |
For example: | For example: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
var ch: char; | var ch: char; | ||
− | + | c: byte; | |
begin | begin | ||
− | ch := 'A'; c := | + | ch := 'A'; c := 65; { are the same action, and are legal } |
− | ch := | + | ch := 65; c := 'A'; { while they are internally the same values, |
+ | direct assignment between Chars and Bytes is illegal } | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | 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 [[coersion|coerced]] to byte by using the [[ord]] function. Byte type values can be coerced to char by using the [[chr]] function. | + | 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 [[coersion|coerced]] to byte by using the [[Ord|ord]] function. Byte type values can be coerced to char by using the [[Chr|chr]] function. |
type chars functions follows the [[ASCII]]. | type chars functions follows the [[ASCII]]. | ||
Line 20: | Line 21: | ||
The above program corrected to legal use: | The above program corrected to legal use: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
var ch: char; | var ch: char; | ||
c: byte; | c: byte; | ||
begin | begin | ||
− | ch := 'A'; | + | ch := 'A'; c := 65; { are the same action, and are legal } |
− | ch := chr( | + | ch := chr(65); c := ord('A'); { now legal } |
+ | ch := Char(65); c := Byte('A'); { also legal and guaranteed to happen at compile time } | ||
end. | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | A [[For|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: | ||
+ | <syntaxhighlight lang=pascal> | ||
+ | var | ||
+ | Loop: Char; | ||
+ | |||
+ | begin | ||
+ | for Loop := 'a' to 'c' do WriteLn(Loop); | ||
+ | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 07:58, 17 October 2023
│
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;
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:
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:
var
Loop: Char;
begin
for Loop := 'a' to 'c' do WriteLn(Loop);
end.
See also
- Character and string types, a detailed reference covering internal memory layout and access options.
simple data types |
|
---|---|
complex data types |