Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Variables and Data Types/ja"

From Lazarus wiki
Jump to navigationJump to search
Line 28: Line 28:
 
* '''char''' データ・タイプは文字を扱う。<tt>'a' 'B' '+'</tt> のようにシングル・クォーテーションでくくる必要がある。標準的な Pascal は16ビットの文字ではなく、8ビットの文字を使う。そのため、ひとつの単一化されたコードシステムで世界中のすべての言語セットを表現するために利用されている Unicode はサポートされていない。
 
* '''char''' データ・タイプは文字を扱う。<tt>'a' 'B' '+'</tt> のようにシングル・クォーテーションでくくる必要がある。標準的な Pascal は16ビットの文字ではなく、8ビットの文字を使う。そのため、ひとつの単一化されたコードシステムで世界中のすべての言語セットを表現するために利用されている Unicode はサポートされていない。
 
* '''WideChar''' は2バイトの文字で(DBCSの要素: Double Byte Character Set)、unicodeを扱える。
 
* '''WideChar''' は2バイトの文字で(DBCSの要素: Double Byte Character Set)、unicodeを扱える。
* Free Pascal は Delphi が行った'''PChar''' タイプの移植をサポートしている。 PChar は Char タイプのポインタとして定義されるが、追加の操作もできるようになっている。 PChar タイプは C スタイルの終端された文字列(C-style null-terminated string)の Pascal の等価版と考えるともっともわかりやすい。すなわち、タイプ PChar の変数は終端文字 (#0) で終わるタイプ Char の配列を示すポインタなのである。 Free Pascal は PChar タイプの定数の初期化と直接割り当てをサポートしている。をped constants, or a direct assignment. For example, the following pieces of code are equivalent:
+
* Free Pascal は Delphi が行った'''PChar''' タイプの移植をサポートしている。 PChar は Char タイプのポインタとして定義されるが、追加の操作もできるようになっている。 PChar タイプは C スタイルの終端された文字列(C-style null-terminated string)の Pascal の等価版と考えるともっともわかりやすい。すなわち、タイプ PChar の変数は終端文字 (#0) で終わるタイプ Char の配列を示すポインタなのである。 Free Pascal は PChar タイプの定数の初期化と直接割り当てをサポートしている。従って、たとえば、以下のコードは同じ意味になる。
 
<syntaxhighlight>
 
<syntaxhighlight>
 
program one;   
 
program one;   
Line 44: Line 44:
 
end.
 
end.
 
</syntaxhighlight>
 
</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 ansistrings (with unlimited length) as in Delphi. And can be declared as:
+
* Free Pascal '''String''' タイプを Turbo Pascal で定義されたと同じようにサポートしている。つまり、サイズの指定がオプションとなっている文字列ということである。また、Delphi にある ansistrings (長さの指定なし)もサポートしている。それらは次のように宣言される。
 
<syntaxhighlight>
 
<syntaxhighlight>
variable_name : string;                    // if no length is given, it defaults to 255
+
variable_name : string;                    // 長さの指定はなし。デフォルトの長さは255
variable_name : string[length];            // where:  1 < length <= 255
+
variable_name : string[length];            // ここで length に入る値は:  1 < length <= 255
 
</syntaxhighlight>
 
</syntaxhighlight>
* The predefined type '''ShortString''' is defined as a string of size 255.
+
* 事前に定義された'''ShortString'''はサイズ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.   
 
* '''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 <tt>WideChars</tt> instead of regular <tt>Chars</tt>.  
 
* '''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>.  

Revision as of 08:50, 11 July 2015

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

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

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

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

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

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

  • integer
  • real
  • char
  • boolean

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

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

  • integer データ・タイプは-32768 から 32767までの整数を扱うことができる。これは16ビットで保存できる符号付きの範囲であり、16ビットCPUが普通だった頃の遺産である。下位互換性の目的で32ビット符号付き integer は longint であり、値の範囲はずっと広い。
  • real データ・タイプは3.4x10-38から3.4x1038であり、加えてマイナス側にも同じ範囲を含む。実数値は仮数と指数を持ち、多少の複雑さを伴うものの科学的表記に似た形でコンピュータ内に保存される。Pascal では実数をコードの中で固定した小数点の表記で表現することもできるし、文字 E で仮数と指数を区切った科学的表記で表現することもできる。従って、 452.134.5213e2 と同じ意味になる。
  • char データ・タイプは文字を扱う。'a' 'B' '+' のようにシングル・クォーテーションでくくる必要がある。標準的な Pascal は16ビットの文字ではなく、8ビットの文字を使う。そのため、ひとつの単一化されたコードシステムで世界中のすべての言語セットを表現するために利用されている Unicode はサポートされていない。
  • WideChar は2バイトの文字で(DBCSの要素: Double Byte Character Set)、unicodeを扱える。
  • Free Pascal は Delphi が行ったPChar タイプの移植をサポートしている。 PChar は Char タイプのポインタとして定義されるが、追加の操作もできるようになっている。 PChar タイプは C スタイルの終端された文字列(C-style null-terminated string)の Pascal の等価版と考えるともっともわかりやすい。すなわち、タイプ PChar の変数は終端文字 (#0) で終わるタイプ Char の配列を示すポインタなのである。 Free Pascal は PChar タイプの定数の初期化と直接割り当てをサポートしている。従って、たとえば、以下のコードは同じ意味になる。
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 はString タイプを Turbo Pascal で定義されたと同じようにサポートしている。つまり、サイズの指定がオプションとなっている文字列ということである。また、Delphi にある ansistrings (長さの指定なし)もサポートしている。それらは次のように宣言される。
variable_name : string;                    // 長さの指定はなし。デフォルトの長さは255
variable_name : string[length];            // ここで length に入る値は:  1 < length <= 255
  • 事前に定義されたShortStringはサイズ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


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