Originale-mail to me for new edition

 

True constants

 

A true constant is a declared identifier whose value cannot change. For example,

const MaxValue = 237;

declares a constant called MaxValue that returns the integer 237. The syntax for declaring a true constant is

const identifier = constantExpression

where identifier is any valid identifier and constantExpression is an expression that the compiler can evaluate without executing your program. (See Constant expressions for more information.)

If constantExpression returns an ordinal value, you can specify the type of the declared constant using a value typecast. For example

const MyNumber = Int64(17);

declares a constant called MyNumber, of type Int64, that returns the integer 17. Otherwise, the type of the declared constant is the type of the constantExpression.

·If constantExpression is a character string, the declared constant is compatible with any string type. If the character string is of length 1, it is also compatible with any character type.

·If constantExpression is a real, its type is Extended. If it is an integer, its type is given by the table below.

 

Range of constant

(hexadecimal)

Range of constant

(decimal)

Type

$8000000000000000..$80000001

2^63..2147483649

Int64

$80000000..$8001

2147483648..32769

Integer

$8000.. $81

32768..129

Smallint

$80..1

128..1

Shortint

0..$7F

0..127

0..127

$80..$FF

128..255

Byte

$0100..$7FFF

256..32767

0..32767

$8000..$FFFF

32768..65535

Word

$10000..$7FFFFFFF

65536..2147483647

0..2147483647

$80000000..$FFFFFFFF

2147483648..4294967295

Cardinal

$100000000..$7FFFFFFFFFFFFFFF

4294967296..2^63

Int64

 

Here are some examples of constant declarations:

const

  Min = 0;

  Max = 100;

  Center = (Max - Min) div 2;

  Beta = Chr(225);

  NumChars = Ord('Z') - Ord('A') + 1;

  Message = 'Out of memory';

  ErrStr = ' Error: ' + Message + '. ';

  ErrPos = 80 - Length(ErrStr) div 2;

  Ln10 = 2.302585092994045684;

  Ln10R = 1 / Ln10;

  Numeric = ['0'..'9'];

  Alpha = ['A'..'Z', 'a'..'z'];

  AlphaNum = Alpha + Numeric;

 

Topic groups

 

See also

About typed constants

Constants: Overview

Resource strings

 

 

译文

 

真实常量

 

真实常量是指一个被声明的标识符,它的值不能改变。例如,

const MaxValue = 237;

这里声明了一个叫做MaxValue的常量,该常量返回整数237。声明真实常量的语法是

const identifier = constantExpression

这里的identifier是一个有效标识符,constantExpression是一个不需要执行程序就能由编译器求值的表达式。(更多信息见常量表达式。)

如果constantExpression返回一个序数值,那么在声明常量时可以强制指定其类型。例如

const MyNumber = Int64(17);

这里声明了一个叫做MyNumber的常量,其类型是Int64,该常量返回整数17。如果不指定类型,那么声明的常量的类型就是表达式constantExpression的类型。

·如果表达式constantExpression是一个字符串,那么声明的常量与任何串类型都是兼容的。如果该字符串的长度是1,那么该常量还与任何字符类型兼容。

·如果表达式constantExpression是一个实数,那么其类型是Extended。如果表达式constantExpression是一个整数,那么其类型的确定遵循下表

 

常量范围(十六进制)

常量范围(十进制)

类型

$8000000000000000..$80000001

2^63..2147483649

Int64

$80000000..$8001

2147483648..32769

Integer

$8000.. $81

32768..129

Smallint

$80..1

128..1

Shortint

0..$7F

0..127

0..127

$80..$FF

128..255

Byte

$0100..$7FFF

256..32767

0..32767

$8000..$FFFF

32768..65535

Word

$10000..$7FFFFFFF

65536..2147483647

0..2147483647

$80000000..$FFFFFFFF

2147483648..4294967295

Cardinal

$100000000..$7FFFFFFFFFFFFFFF

4294967296..2^63

Int64

 

下面是一些常量声明的范例:

const

  Min = 0;

  Max = 100;

  Center = (Max - Min) div 2;

  Beta = Chr(225);

  NumChars = Ord('Z') - Ord('A') + 1;

  Message = 'Out of memory';

  ErrStr = ' Error: ' + Message + '. ';

  ErrPos = 80 - Length(ErrStr) div 2;

  Ln10 = 2.302585092994045684;

  Ln10R = 1 / Ln10;

  Numeric = ['0'..'9'];

  Alpha = ['A'..'Z', 'a'..'z'];

  AlphaNum = Alpha + Numeric;

 

主题组

 

相关主题

关于类型常量

常量:概述

资源串

 

 

编者注

 

对于串常量(包括长度是1的字符串和长度是0的空串),除了上面所讲的与任何串类型兼容之外,还与PChar等字符指针类型兼容。例如,Windows单元(预定义单元)中提供的MessageBox函数的声明如下:

 

function MessageBox(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;

 

函数要求第二、三个参数均为PChar类型。对于下面的语句,第一、三次调用是合法的,而第二次调用是非法的:

 

const

  Welcome = 'Hello';

  Title   = 'Information';

  Blank   = '';

 

var

  Hello: string = 'Hello';

  Info: string  = 'Information';

 

...

 

  MessageBox(Handle, Welcome, Blank, ID_OK);//合法调用

  MessageBox(Handle, Hello, Info, ID_OK);//非法调用

  MessageBox(Handle, PChar(Hello), PChar(Info), ID_OK);//合法调用

 

严格来说,上面的合法调用是编译器自动对串常量进行了转换,即将串常量转换为字符指针PChar类型传递给函数。而对于指明了类型的串变量,则不能直接作为PChar参数传递,但可以直接通过类型转换来实现。这也表明了Object Pascal对字符和串的强大处理功能。