What are Pascal variables?

Overview

A variable is like a container that stores some value and maps it on the memory with a specific name to access it. The variable should be declared before using it. It is a good practice to initialize each variable with a default value to remove garbage.

Data types

Pascal provides several intrinsic data types. However, we can derive our own data types as well.

The data types of variables in Pascal are as follows::

  • Integer: An integer data type variable holds a value that is a whole number.
  • Character: A character data type variable carries a single character.
  • Real: A real data type variable has a value that is a floating point number.
  • Boolean: A boolean variable carries only two values, either true or false.

Syntax

The syntax for variable declaration is given below:

var
variable_list : type;

Code example

The following example shows how to initialize and declare the variable in Pascal:

program task;
var number1 : integer;
letter : char;
begin
number1 := 23;
letter := 'W';
writeln(' ');
writeln('****************************');
writeln('OUTPUT :number1 = ', number1);
writeln(' letter = ', letter);
writeln('****************************');
end.

Code explanation

  • Line 1: We name the program.
  • Lines 2 and 3: We declare variables named number1 and letter .
  • Line 4: We begin the program.
  • Lines 5 and 6: We assign values to variables.
  • Lines 9 and 10: We display the values of variables to the console.
  • Line 12: We end the program.

Note: The following warning will appear:

/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?

It can be ignored. It has been fixed in the development build FPC version 3.1.1 and latest, which have not been released officially.

Copyright ©2024 Educative, Inc. All rights reserved