Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 1/Variables and Data Types

Sent here from Variables and Data Types.

한국어 · Deutsch · English · Español · Français · Български · Русский · 中文(中国) · 日本語

1D - Variables and Data Types (author: Tao Yue, state: changed)

Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used:

pascal
var
  IdentifierList1 : DataType1;
  IdentifierList2 : DataType2;
  IdentifierList3 : DataType3;
  ...

IdentifierList is a series of identifiers, separated by commas (,). All identifiers in the list are declared as being of the same data type.

The basic data field data types in Pascal include:

Standard Pascal does not make provision for the string data type, but most modern compilers do. Experienced Pascal programmers also use pointers for dynamic memory allocation, objects for object-oriented programming, and many others, but this gets you started.

More information on Pascal data types:

pascal
program one;  
var P : PChar;  
begin  
  P := 'This is a null-terminated string.';  
  WriteLn (P);  
end.
pascal
program two;  
const P : PChar = 'This is a null-terminated string.';  
begin  
  WriteLn (P);  
end.
pascal
variable_name : string;                    // if no length is given, it defaults to 255
variable_name : string[length];            // where:  1 < length <= 255

An example of declaring several variables is:

pascal
var
  Age, Year, Grade : integer;
  Circumference : real;
  LetterGrade : char;
  DidYouFail : boolean;

From the FPC manual

Type Range Bytes
Byte 0 .. 255 1
Shortint -128 .. 127 1
Smallint -32768 .. 32767 2
Word 0 .. 65535 2
Integer smallint or longint 2 or 4
Cardinal longword 4
Longint -2147483648 .. 2147483647 4
Longword 0..4294967295 4
Int64 -9223372036854775808 .. 9223372036854775807 8
QWord 0 .. 18446744073709551615 8

integer types

Free Pascal does automatic type conversion in expressions where different kinds of integer types are used.

Type Range Significant digits Bytes
Real platform dependent ??? 4 or 8
Single 1.5E-45 .. 3.4E38 7-8 4
Double 5.0E-324 .. 1.7E308 15-16 8
Extended* 1.9E-4932 .. 1.1E4932 19-20 10
Comp -2E64+1 .. 2E63-1 19-20 8
Currency -922337203685477.5808 922337203685477.5807 8

real types

Type Bytes Ord(True)
Boolean 1 1
ByteBool 1 Any nonzero value
WordBool 2 Any nonzero value
LongBool 4 Any nonzero value

boolean types


From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 1/Variables and Data Types", as it stood on 2022-08-20 after a change by Kai Burghardt, 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.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.