Sign in Join
Wiki copytemporary

Char: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Char

OldNewLine
1+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.
2+
3+For example:
4+
5+``` pascal
6+ var ch: char;
7+ c: byte;
8+
9+ begin
10+ ch := 'A'; c := 65; { are the same action, and are legal }
11+ ch := 65; c := 'A'; { while they are internally the same values,
12+ direct assignment between Chars and Bytes is illegal }
13+ end.
14+```
15+
16+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.
17+
18+type chars functions follows the [[ASCII]].
19+
20+The above program corrected to legal use:
21+
22+``` pascal
23+ var ch: char;
24+ c: byte;
25+
26+ begin
27+ ch := 'A'; c := 65; { are the same action, and are legal }
28+ ch := chr(65); c := ord('A'); { now legal }
29+ ch := Char(65); c := Byte('A'); { also legal and guaranteed to happen at compile time }
30+ end.
31+```
32+
33+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:
34+
35+``` pascal
36+var
37+ Loop: Char;
38+
39+begin
40+ for Loop := 'a' to 'c' do WriteLn(Loop);
41+end.
42+```
43+
44+## See also
45+
46+- [[Character and string types]], a detailed reference covering internal memory layout and access options.
47+
48+---
49+*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](/wiki-contact).*

Back to history