Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Variables and Data Types/ja"
(Created page with "{{Variables_and_Data_Types}} 1D - Variables and Data Types (author: Tao Yue, state: ''changed'') Variables are similar to constants, but their values can be changed as the p...") |
|||
Line 1: | Line 1: | ||
{{Variables_and_Data_Types}} | {{Variables_and_Data_Types}} | ||
− | 1D - | + | 1D - 変数とデータタイプ (著者: Tao Yue, 状態: 変更あり) |
− | + | 変数は定数と似ているが、その値はプログラム実行中に変化することができる。Pascalでは変数は利用する前に最初に定義されていなくてはならない。 | |
<syntaxhighlight> | <syntaxhighlight> | ||
var | var | ||
Line 11: | Line 11: | ||
... | ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | <tt>IdentifierList</tt> | + | <tt>IdentifierList</tt> は一連の識別子でカンマ(<tt>,</tt>)で区切られる。リストの識別子はすべて同じデータ・タイプとして宣言されている。 |
− | + | Pascal における基本データ・タイプには以下のものが含まれる。 | |
* integer | * integer | ||
Line 20: | Line 20: | ||
* boolean | * boolean | ||
− | + | 標準的な Pascal では文字データ・タイプを事前に用意しておく必要はないが、ほとんどの現代のコンパイラでは必要となる。経験豊富な Pascal プログラムは動的メモリー割り当てのためにポインタを使ったり、オブジェクト指向プログラミングにオブジェクトを使用したり、他にもいろいろ使用するかもしれない。しかし、ここが始まりとなる。 | |
− | + | Pascal データ・タイプに関するより詳細な情報: | |
− | * | + | * '''integer''' データ・タイプは<tt>-32768</tt> から <tt>32767</tt>までの整数を扱うことができる。これは16ビットで保存できる符号付きの範囲であり、16ビットCPUが普通だった頃の遺産である。下位互換性の目的で32ビット符号付き integer は longint であり、値の範囲はずっと広い。 |
− | * | + | * '''real''' データ・タイプは<tt>3.4x10<sup>-38</sup></tt>から<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 '''WideChar''' is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a unicode character. | * The '''WideChar''' is a two-byte character (an element of a DBCS: Double Byte Character Set) and can hold a unicode character. |
Revision as of 08:57, 9 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であり、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.
- 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:
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
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 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 |
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 |