Difference between revisions of "Char"
m (formatting improved) |
|||
Line 4: | Line 4: | ||
For example: | For example: | ||
− | < | + | <delphi> |
− | + | var ch: char; | |
− | + | c: byte; | |
− | + | ||
− | + | begin | |
− | + | ch := 'A'; c := 64; { are the same action, and are legal } | |
− | + | ch := 64; c := 'A'; { while they are the same action, this is illegal } | |
− | + | end. | |
− | </ | + | </delphi> |
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]] function. Byte type values can be coerced to char by using the [[chr]] function. | ||
Line 20: | Line 20: | ||
The above program corrected to legal use: | The above program corrected to legal use: | ||
− | < | + | <delphi> |
− | + | var ch: char; | |
− | + | c: byte; | |
− | + | ||
− | + | begin | |
− | + | ch := 'A'; c := 64; { are the same action, and are legal } | |
− | + | ch := chr(64); c := ord('A'); { now legal } | |
− | + | end. | |
− | </ | + | </delphi> |
{{Data types}} | {{Data types}} | ||
[[category:Pascal]] | [[category:Pascal]] |
Revision as of 11:17, 8 July 2007
│
Deutsch (de) │
English (en) │
español (es) │
français (fr) │
italiano (it) │
русский (ru) │
A char is a single character. A byte and a char are the same thing, except a char can 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: <delphi>
var ch: char; c: byte;
begin ch := 'A'; c := 64; { are the same action, and are legal } ch := 64; c := 'A'; { while they are the same action, this is illegal } end.
</delphi>
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:
<delphi>
var ch: char; c: byte;
begin ch := 'A'; c := 64; { are the same action, and are legal } ch := chr(64); c := ord('A'); { now legal } end.
</delphi>
simple data types |
|
---|---|
complex data types |