Last Updated:

Character type in Pascal

 

 

A character type is a data type that stores a symbol as a value. In the Pascal language, in variables, it is declared with the keyword char. The size of a variable of this type in Pascal is 1 byte. Thus, there can be a total of 256 different values for this type. And the characters used in the char type are ASCII encoded.

An example of declaring a variable of type char:

Code:var c1, c2:char;
begin
c1:= ‘a’; explicit c2
:= #97;// by the number of the encoded
character writeln('Char 1: ', c1);
writeln(‘Char 2: ‘, c2);
end.

Inference:

Char 1: a

Char 2: a

Differences from string type

Unlike the string type string, the character type contains only one character. The string type is essentially an array of variables of type char. A character type, in turn, can be thought of as a string in functions that target the string type, such as insert and copy, for example.

An example of using the insert and copy functions using the char type:

Code:var s:string;
c: char;
begin
s:=’String ‘;
c:= ‘A’;
insert(c,s,8);
writeln(‘String 1: ‘, s);
s:=copy(c,1,1);
writeln(‘String 2: ‘, s);
end.

Inference:

String 1: String A
String 2: A

Convert a character type to others

The ord function retrieves the character code and returns a byte value. The chr function works in reverse. Knowing the code from the ASCII table, with its help you can get the symbol itself.

Example of ord and chr:

Code:varc: char;
b: byte;
begin
c:= ‘a’;
b:= ord(c);
writeln(‘Ord(c): ‘, b);
c:= chr(b+1);
writeln(‘Chr(b+1): ‘, c);
end.

Inference:

Ord(c): 97
Chr(b+1): b

To string, the character type is converted implicitly using the assignment operator.

An example of a program that implements the conversion of char to string:

Code:var c: char;
s: string;
begin
c := ‘a’;
s := c;
writeln(‘String: ‘, s);
end.

Inference:

String: a

Functions for type Char

The succ function returns the following character in the table relative to the received value. It works similarly to the integer data type procedure. The pred function returns the previous character in the ASCII table. It is equivalent to dec for integers.

An example of a program with succ and pred functions:

Code:var c: char;
begin
c := ‘c’;
writeln(‘Succ(c): ‘, succ(c));
writeln(‘Pred(c): ‘, pred(c));
end.

Inference:

Succ(c): d
Pred(c): b

The inc and dec procedures are also applicable to a character data type. Their advantage over succ and pred is the ability to change the current value of the character type not only by one, but also by a larger number.

The upcase and lowercase functions are designed to change the case of a character. The first converts a lowercase letter to uppercase, the second - uppercase to lowercase.

An example of how these functions work:

Code:begin
writeln('Upcase(c): ', upcase('c'));
writeln(‘Lowercase(C): ‘, lowercase(‘C’));
end.

Inference:

Upcase(c): C
Lowercase(C): c

Operations on char data

For a character type, as well as for other data types, comparison operations (<, >, <=, >=, =, <>) are applicable. The comparison is made taking into account the character numbers in the table. Unlike byte, char is not involved in addition and subtraction operations.

An example of a program using comparison operations:

Code:var b, c:char;
begin
b := ‘b’;
c := ‘c’;
writeln(‘ b < c: ‘, b < c);
writeln(‘ b > c: ‘, b > c);
writeln(‘ b <> c: ‘, b <> c);
writeln(‘ b = c: ‘, b = c);
writeln(‘ b <= c: ‘, b <= c);
writeln(‘ b >= c: ‘, b >= c);
end.

Inference:

b < c: TRUE
b > c: FALSE
b <> c: TRUE
b = c: FALSE
b <= c: TRUE
b >= c: FALSE

A variable of a character type can also be used as a loop counter.

An example of using the char type in a loop:

Code:var i: char;
begin
for i:= ‘a’ to ‘d’ do
write (i, ‘ ‘);
end.

Inference:

a b c d