Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Variables and Data Types"
Line 1: | Line 1: | ||
− | 1D - Variables and Data Types (author: Tao Yue, state: | + | 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: | ||
Line 23: | Line 23: | ||
* 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 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 '''real''' data type has a range from <tt>3.4x10-38</tt> to <tt>3. | + | * 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 '''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 '''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 '''boolean''' data type can have only two values: '''TRUE''' and '''FALSE''' | * The '''boolean''' data type can have only two values: '''TRUE''' and '''FALSE''' | ||
Line 35: | Line 35: | ||
DidYouFail : Boolean; | DidYouFail : Boolean; | ||
</delphi> | </delphi> | ||
+ | |||
+ | '''From the FPC manual''' | ||
+ | |||
+ | {| border=1 cellpadding=3 cellspacing=0 | ||
+ | |- style="background:lightgrey" | ||
+ | |+'''integer types''' | ||
+ | !Type !!Range !!Bytes | ||
+ | |- | ||
+ | |Byte ||align="center"| 0 .. 255 || 1 | ||
+ | |- | ||
+ | |Shortint ||align="center"| -128 .. 127 || 1 | ||
+ | |- | ||
+ | |Smallint ||align="center"|-32768 .. 32767 || 2 | ||
+ | |- | ||
+ | |Word ||align="center"|0 .. 65535 ||2 | ||
+ | |- | ||
+ | |Integer ||align="center"|smallint or longint ||2 or 4 | ||
+ | |- | ||
+ | |Cardinal ||align="center"|longword ||4 | ||
+ | |- | ||
+ | |Longint ||align="center"|-2147483648 .. 2147483647 ||4 | ||
+ | |- | ||
+ | |Longword ||align="center"|0..4294967295 ||4 | ||
+ | |- | ||
+ | |Int64 ||align="center"|-9223372036854775808 .. 9223372036854775807 ||8 | ||
+ | |- | ||
+ | |QWord ||align="center"|0 .. 18446744073709551615 ||8 | ||
+ | |} | ||
+ | Free Pascal does automatic type conversion in expressions where different kinds of integer types are used. | ||
+ | |||
+ | {| border=1 cellpadding=3 cellspacing=0 | ||
+ | |- style="background:lightgrey" | ||
+ | |+'''real types''' | ||
+ | !Type !!Range !!Significant digits !!Bytes | ||
+ | |- | ||
+ | |Real ||align="center"|platform dependant ||align="center"|??? ||4 or 8 | ||
+ | |- | ||
+ | |Single ||align="center"|1.5E-45 .. 3.4E38 ||align="center"|7-8 ||4 | ||
+ | |- | ||
+ | |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 | ||
+ | |- | ||
+ | |Comp ||align="center"|-2E64+1 .. 2E63-1 ||align="center"|19-20 ||8 | ||
+ | |- | ||
+ | |Currency ||align="center"|-922337203685477.5808 ||align="center"|922337203685477.5807 ||8 | ||
+ | |} | ||
+ | |||
+ | |||
+ | {| border=1 cellpadding=3 cellspacing=0 | ||
+ | |- style="background:lightgrey" | ||
+ | |+'''boolean types''' | ||
+ | !Type !! Bytes !! Ord(True) | ||
+ | |- | ||
+ | |Boolean ||align="center"|1 ||1 | ||
+ | |- | ||
+ | |ByteBool ||align="center"|1 ||Any nonzero value | ||
+ | |- | ||
+ | |WordBool ||align="center"|2 ||Any nonzero value | ||
+ | |- | ||
+ | |LongBool ||align="center"|4 ||Any nonzero value | ||
+ | |} | ||
{|style=color-backgroud="white" cellspacing="20" | {|style=color-backgroud="white" cellspacing="20" |
Revision as of 16:33, 7 January 2010
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: <delphi> var
IdentifierList1 : DataType1; IdentifierList2 : DataType2; IdentifierList3 : DataType3; ...
</delphi> 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 types in Pascal include:
- integer
- 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.
More information on Pascal data types:
- The integer data type can contain integers 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.
- 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 boolean data type can have only two values: TRUE and FALSE
An example of declaring several variables is: <delphi> var
age, year, grade : integer; circumference : real; LetterGrade : char; DidYouFail : Boolean;
</delphi>
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 |
Free Pascal does automatic type conversion in expressions where different kinds of integer types are used.
Type | Range | Significant digits | Bytes |
---|---|---|---|
Real | platform dependant | ??? | 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 |
Type | Bytes | Ord(True) |
---|---|---|
Boolean | 1 | 1 |
ByteBool | 1 | Any nonzero value |
WordBool | 2 | Any nonzero value |
LongBool | 4 | Any nonzero value |
previous | contents | next |