Pascal. Boolean types. Character types. String types

Boolean types. Character types. String types

This topic is a continuation of the topic:


Contents


Search other resources:

1. Boolean types

There are 4 boolean types used in Pascal:

  • Boolean – a variable of this type takes up 1 byte of memory;
  • ByteBool – takes 1 byte of memory;
  • WordBool – takes 2 bytes of memory;
  • LongBool – takes 4 bytes of memory.

Boolean elements can only take two boolean values: True and False. In boolean types, False is 0 in all bytes (zero). True matches any nonzero value. Boolean types are ordinal types, so the standard functions Ord, Pred, Succ can be used for them.

 

2. An example of using constants and variables of boolean types

Below is an example of declaring and using constants and variables of boolean types for an application of type Console Application of the Delphi programming system.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

// Boolean constant declaration
const
  OK = True;

var
  // Declare variables of boolean types
  b:Boolean;
  bb:ByteBool;
  wb:WordBool;
  lb:LongBool;

begin
  // Assigning values to variables of boolean types
  b:=True;
  bb:=False;
  wb:=True and False;
  lb:=(10>8) and True;

  Writeln('b = ', b);
  Writeln('bb = ', bb);
  Writeln('wb = ', wb);
  Writeln('lb = ', lb);
  Writeln('OK = ', OK);
  ReadLn;
end.

The result of the program

b = TRUE
bb = FALSE
wb = FALSE
lb = TRUE
OK = TRUE

 

3. An example of using the Ord, Succ, Pred functions for boolean types

The example demonstrates the use of the Ord, Succ, Pred functions for the Boolean and LongBool types.

program TestBoolTypes;

uses
  crt;

var
  // Declaring boolean variables
  b:Boolean;
  lb:LongBool;

begin
  // Examples of using the Ord, Pred, Succ functions for some boolean types
  // Boolean type
  b := True;
  Writeln('b = ', b);
  Writeln('Ord(b) = ', Ord(b));
  Writeln('Pred(b) = ', Pred(b));
  Writeln('Succ(b) = ', Succ(b));

  // LongBool type
  lb := False;
  Writeln('lb = ', lb);
  Writeln('Ord(lb) = ', Ord(lb));
  Writeln('Pred(lb) = ', Pred(lb));
  Writeln('Succ(lb) = ', Succ(lb));
end.

Program result

b = TRUE
Ord(b) = 1
Pred(b) = FALSE
Succ(b) = TRUE
lb = FALSE
Ord(lb) = 0
Pred(lb) = TRUE
Succ(lb) = TRUE

 

4. Character types. Example

The following character types are implemented in Free Pascal and Delphi:

  • Char, WideChar represent a type using the Unicode encoding. Variables of this type take up 2 bytes of memory. In early versions of Pascal, the Char type was 1 byte;
  • AnsiChar – a type that defines character values that occupy 1 byte in memory.

Example. The example declares and uses constants and variables of the character type Char, WideChar, AnsiChar.

program TestOrdinalTypes;

uses
  crt;

// Examples of declaring constants of a character type
const
  SpaceKey = #32; // Spacebar
  FirstLetter = 'A';

// Declaring variables of character types
var
  c : Char;
  wc : WideChar;
  ac : AnsiChar;
  code : Integer;

begin
  // Assign values to variables of character types
  c := '+';
  wc := '5'; // цифра 5
  ac := '~';

  // Print the values of variables of a character type
  Writeln('c = ', c);
  Writeln('wc = ', wc);
  Writeln('ac = ', ac);

  // Use functions Ord, Chr, Pred, Succ
  // Get the code for the number '5'
  code := Ord('5'); // code = 53
  Writeln('Ord(''5'') = ', code);

  // Get the character that has code 48
  c := Chr(48);
  Writeln('Chr(48) = ', c);

  // Assign character with code 50 to wc variable, another way
  wc := #50;
  Writeln('#50 = ', wc);
end.

Program result

c = +
wc = 5
ac = ~
Ord('5') = 53
Chr(48) = 0
#50 = 2

 

5. String types

In classical Pascal, string types are two basic standard types:

  • String. Strings of type String are implemented in all modifications of the Pascal language;
  • PChar – this string type has been introduced since Turbo Pascal version 7.0. The PChar type represents a string format in which each string ends with a ‘\0’ character. Such strings are also called ASCIIZ strings. The PChar type is actually a pointer type that has a description.
type
  PChar = ^Char;

In addition to the above string types, a number of additional types have been introduced in the new Delphi, Free Pascal (Lazarus) systems:

  • ShortString – defined as an array of characters of type String[255];
  • UnicodeString – defined as String. Single characters of this type support Unicode encoding and are 2 bytes long;
  • WideString is an array of characters. Each character takes up 2 bytes of memory;
  • AnsiString is an array of characters. Each character takes up 1 byte memory.

 

6. An example of declaring and using variables of string types

 

program TestOrdinalTypes;

uses
  crt;

// Declare constants of type String
const
  HelloStr = 'Hello, world!';
  Yes = 'Yes';
  Cancel = 'Cancel';

var
  pC : PChar;
  s : String;
  ss : ShortString;
  us : UnicodeString;
  ws : WideString;
  ast : AnsiString;

begin
  // Null-terminated strings (ASCIIZ strings)
  pC := 'PChar';
  Writeln(pC);

  // Strings that are not null terminated
  s := 'String';
  Writeln(s);

  ss := 'ShortString';
  Writeln(ss);

  us := 'UnicodeString';
  Writeln(us);

  ws := 'WideString';
  Writeln(ws);

  ast := 'AnsiString';
  Writeln(ast);

  Readln;
end.

Program result

PChar
String
ShortString
UnicodeString
WideString
AnsiString

 


Related topics