Basic Pascal Tutorial/Chapter 1/Variables and Data Types/ja

1D - 変数とデータタイプ (著者: Tao Yue, 状態: 変更あり)

変数は定数と似ているが、その値はプログラム実行中に変化することができる。Pascalでは変数は利用する前に最初に定義されていなくてはならない。

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

IdentifierList は識別子のリストでカンマ(,)で区切られる。リストの識別子はすべて同じデータ・タイプとして宣言されている。

Pascal における基本データ・タイプには以下のものが含まれる。

標準的な Pascal では文字データ・タイプを事前に用意しておく必要はないが、ほとんどの現代のコンパイラでは必要となる。経験豊富な Pascal プログラムは動的メモリー割り当てのためにポインタを使ったり、オブジェクト指向プログラミングにオブジェクトを使用したり、他にもいろいろ使用するかもしれない。しかし、ここが始まりとなる。

Pascal データ・タイプに関する、より詳細な情報:

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.
variable_name : string;                    // 長さの指定はなし。デフォルトの長さは255
variable_name : string[length];            // ここで length に入る値は:  1 < length <= 255

いくつかの変数を定義する例を以下に挙げる。

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

FPC マニュアルから

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 は異なる種類の整数タイプが用いられたときには自動的に表現上のタイプ変換を行う。

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

boolean types

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