Difference between revisions of "Variables and Data Types"

From Lazarus wiki
m (Added a template to this page since the related translation has been created.)
m (Added note about Extended on Win64 and non-Intel targets)
 
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{Variables_and_Data_Types}}
 
{{Variables_and_Data_Types}}
 +
{{TYNavigator|Constants|Assignment_and_Operations}}
  
 
1D - Variables and Data Types (author: Tao Yue, state: ''changed'')
 
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:
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
var
 
var
 
   IdentifierList1 : DataType1;
 
   IdentifierList1 : DataType1;
Line 11: Line 13:
 
   ...  
 
   ...  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
<tt>IdentifierList</tt> is a series of identifiers, separated by commas (<tt>,</tt>). All identifiers in the list are declared as being of the same data type.
 
<tt>IdentifierList</tt> is a series of identifiers, separated by commas (<tt>,</tt>). All identifiers in the list are declared as being of the same data type.
  
The basic data types in Pascal include:
+
The basic [[Data field|data field]] [[Data type|data types]] in Pascal include:
  
* integer
+
* [[Integer]]
* real
+
* [[Word]]
* char
+
* [[Longint|LongInt]]
* boolean
+
* [[Real]]
 +
* [[Char]]
 +
* [[Boolean]]
  
 
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.
 
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.
Line 24: Line 29:
 
More information on Pascal data types:
 
More information on Pascal data types:
  
* The '''integer''' data type can contain integers from <tt>-32768</tt> to <tt>32767</tt>. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values.
+
* The [[Integer]] data type can contain whole numbers. the size of an integer depends on the compiler and the processor. On PCs before the 80386, "integer" meant 16-bit  whole numbers in the range from <tt>-32768</tt> to <tt>32767</tt>. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values, <tt>2147483647</tt> to <tt>-2147483648</tt>.
* The '''real''' data type has a range from <tt>3.4x10<sup>-38</sup></tt> to <tt>3.4x10<sup>38</sup></tt>, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character <tt>E</tt> separating the mantissa from the exponent. Thus, <tt>452.13</tt> is the same as <tt>4.5213e2</tt>
+
* The [[Word]] data type is a 16-bit unsigned integer, which has a range of <tt>0</tt> to <tt>65535</tt>.
* The '''char''' data type holds characters. Be sure to enclose them in single quotes, like so: <tt>'a' 'B' '+'</tt> Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
+
* The [[Real]] data type has a range from <tt>3.4x10<sup>-38</sup></tt> to <tt>3.4x10<sup>38</sup></tt>, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character <tt>E</tt> separating the mantissa from the exponent. Thus, <tt>452.13</tt> is the same as <tt>4.5213e2</tt>
* The '''WideChar''' is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a unicode character.
+
* The [[Char]] data type holds characters. Be sure to enclose them in single quotes, like so: <tt>'a' 'B' '+'</tt> Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
* Free Pascal supports the Delphi implementation of the '''PChar''' type. PChar is defined as a pointer to a Char type, but allows additional operations. The PChar type can be understood best as the Pascal equivalent of a C-style null-terminated string, i.e. a variable of type PChar is a pointer that points to an array of type Char, which is ended by a null-character (#0). Free Pascal supports initializing of PChar typed constants, or a direct assignment. For example, the following pieces of code are equivalent:  
+
* The [[WideChar]] is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a Unicode character. Note: some Unicode characters require two WideChars. See ''UTF-16''.
* Free Pascal supports the '''String''' type as it is defined in Turbo Pascal: a sequence of characters with an optional size specification. It also supports ansistrings (with unlimited length) as in Delphi. And can be declared as:
+
* Free Pascal supports the Delphi implementation of the [[PChar]] data type. PChar is defined as a pointer to a Char type, but allows additional operations. The PChar type can be understood best as the Pascal equivalent of a C-style null-terminated string, i.e. a variable of type PChar is a pointer that points to an array of type Char, which is ended by a null-character (#0). Free Pascal supports initializing of PChar typed constants, or a direct assignment. For example, the following pieces of code are equivalent:  
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 +
program one; 
 +
var P : PChar; 
 +
begin 
 +
  P := 'This is a null-terminated string.'; 
 +
  WriteLn (P); 
 +
end.
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang=pascal>
 +
program two; 
 +
const P : PChar = 'This is a null-terminated string.'; 
 +
begin 
 +
  WriteLn (P); 
 +
end.
 +
</syntaxhighlight>
 +
 
 +
* Free Pascal supports the [[String]] type as it is defined in Turbo Pascal: a sequence of characters with an optional size specification. It also supports [[Ansistring|AnsiStrings]] (with unlimited length) as in Delphi. And can be declared as:
 +
 
 +
<syntaxhighlight lang=pascal>
 
variable_name : string;                    // if no length is given, it defaults to 255
 
variable_name : string;                    // if no length is given, it defaults to 255
 
variable_name : string[length];            // where:  1 < length <= 255
 
variable_name : string[length];            // where:  1 < length <= 255
 
</syntaxhighlight>
 
</syntaxhighlight>
* The predefined type '''ShortString''' is defined as a string of size 255.
+
 
* '''AnsiStrings''' are strings that have no length limit. They are reference counted and are guaranteed to be null terminated. Internally, an ansistring is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.   
+
* The predefined type [[Shortstring|ShortString]] is defined as a string of size 255.
* '''Widestrings''' (used to represent unicode character strings) are implemented in much the same way as ansistrings: reference counted, null-terminated arrays, only they are implemented as arrays of <tt>WideChars</tt> instead of regular <tt>Chars</tt>.  
+
* [[Ansistring|AnsiStrings]] are strings that have no length limit. They are reference counted and are guaranteed to be null terminated. Internally, an ansistring is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.   
* The '''boolean''' data type can have only two values: '''TRUE''' and '''FALSE'''
+
* [[Widestrings|WideStrings]] (used to represent unicode character strings) are implemented in much the same way as ansistrings: reference counted, null-terminated arrays, only they are implemented as arrays of <tt>WideChars</tt> instead of regular <tt>Chars</tt>.  
 +
* The [[Boolean]] data type can have only two values: '''TRUE''' and '''FALSE'''
  
 
An example of declaring several variables is:
 
An example of declaring several variables is:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
var
 
var
 
   age, year, grade : integer;
 
   age, year, grade : integer;
Line 80: Line 107:
 
!Type !!align="center"|Range !!Significant digits !!Bytes
 
!Type !!align="center"|Range !!Significant digits !!Bytes
 
|-
 
|-
|Real ||align="center"|platform dependant ||align="center"|??? ||4 or 8
+
|Real ||align="center"|platform dependent ||align="center"|??? ||4 or 8
 
|-
 
|-
 
|Single ||align="center"|1.5E-45 .. 3.4E38 ||align="center"|7-8 ||4
 
|Single ||align="center"|1.5E-45 .. 3.4E38 ||align="center"|7-8 ||4
Line 86: Line 113:
 
|Double ||align="center"|5.0E-324 .. 1.7E308 ||align="center"|15-16 ||8
 
|Double ||align="center"|5.0E-324 .. 1.7E308 ||align="center"|15-16 ||8
 
|-
 
|-
|Extended ||align="center"|1.9E-4932 .. 1.1E4932 ||align="center"|19-20 ||10
+
|Extended* ||align="center"|1.9E-4932 .. 1.1E4932 ||align="center"|19-20 ||10
 
|-
 
|-
 
|Comp ||align="center"|-2E64+1 .. 2E63-1 ||align="center"|19-20 ||8
 
|Comp ||align="center"|-2E64+1 .. 2E63-1 ||align="center"|19-20 ||8
Line 92: Line 119:
 
|Currency ||align="center"|-922337203685477.5808 ||align="center"|922337203685477.5807 ||8
 
|Currency ||align="center"|-922337203685477.5808 ||align="center"|922337203685477.5807 ||8
 
|}
 
|}
 +
 +
* Note that for Windows 64 bits and non-Intel targets Extended is an alias for Double.
 
   
 
   
  
Line 107: Line 136:
 
|}
 
|}
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Constants|Assignment_and_Operations}}
|[[Constants|previous]] 
 
|[[Contents|contents]]
 
|[[Assignment_and_Operations|next]]
 
|}
 
 
 
[[Category:Pascal]]
 
[[Category: Object Pascal Introduction]]
 

Latest revision as of 16:32, 4 August 2020

български (bg) Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

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:

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:

  • The Integer data type can contain whole numbers. the size of an integer depends on the compiler and the processor. On PCs before the 80386, "integer" meant 16-bit whole numbers in the range from -32768 to 32767. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values, 2147483647 to -2147483648.
  • The Word data type is a 16-bit unsigned integer, which has a range of 0 to 65535.
  • The Real data type has a range from 3.4x10-38 to 3.4x1038, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character E separating the mantissa from the exponent. Thus, 452.13 is the same as 4.5213e2
  • The Char data type holds characters. Be sure to enclose them in single quotes, like so: 'a' 'B' '+' Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
  • The WideChar is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a Unicode character. Note: some Unicode characters require two WideChars. See UTF-16.
  • Free Pascal supports the Delphi implementation of the PChar data type. PChar is defined as a pointer to a Char type, but allows additional operations. The PChar type can be understood best as the Pascal equivalent of a C-style null-terminated string, i.e. a variable of type PChar is a pointer that points to an array of type Char, which is ended by a null-character (#0). Free Pascal supports initializing of PChar typed constants, or a direct assignment. For example, the following pieces of code are equivalent:
program one;  
var P : PChar;  
begin  
  P := 'This is a null-terminated string.';  
  WriteLn (P);  
end.
program two;  
const P : PChar = 'This is a null-terminated string.';  
begin  
  WriteLn (P);  
end.
  • Free Pascal supports the String type as it is defined in Turbo Pascal: a sequence of characters with an optional size specification. It also supports AnsiStrings (with unlimited length) as in Delphi. And can be declared as:
variable_name : string;                    // if no length is given, it defaults to 255
variable_name : string[length];            // where:  1 < length <= 255
  • The predefined type ShortString is defined as a string of size 255.
  • AnsiStrings are strings that have no length limit. They are reference counted and are guaranteed to be null terminated. Internally, an ansistring is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.
  • WideStrings (used to represent unicode character strings) are implemented in much the same way as ansistrings: reference counted, null-terminated arrays, only they are implemented as arrays of WideChars instead of regular Chars.
  • The Boolean data type can have only two values: TRUE and FALSE

An example of declaring several variables is:

var
  age, year, grade : integer;
  circumference : real;
  LetterGrade : char;
  DidYouFail : Boolean;

From the FPC manual

integer types
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

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

real types
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
  • Note that for Windows 64 bits and non-Intel targets Extended is an alias for Double.


boolean types
Type Bytes Ord(True)
Boolean 1 1
ByteBool 1 Any nonzero value
WordBool 2 Any nonzero value
LongBool 4 Any nonzero value
 ◄   ▲   ►