常量
优质
小牛编辑
140浏览
2023-12-01
常量是在程序执行期间保持不变的实体。 Pascal只允许声明以下类型的常量 -
- Ordinal types
- 设置类型
- 指针类型(但唯一允许的值是Nil)。
- Real types
- Char
- String
声明常量
声明常量的语法如下 -
const
identifier = constant_value;
下表提供了一些有效的常量声明的示例 -
Real type constant
Sr.No | 常数类型和示例 |
---|---|
1 | Ordinal(Integer)type constant valid_age = 21; |
2 | Set type constant 元音=(A,E,I,O,U)的集合; |
3 | Pointer type constant P =无; |
4 | e = 2.7182818; velocity_light = 3.0E + 10; |
5 | Character type constant Operator ='+'; |
6 | String type constant 总统='约翰尼德普'; |
以下示例说明了这一概念 -
program const_circle (input,output);
const
PI = 3.141592654;
var
r, d, c : real; {variable declaration: radius, dia, circumference}
begin
writeln('Enter the radius of the circle');
readln(r);
d := 2 * r;
c := PI * d;
writeln('The circumference of the circle is ',c:7:2);
end.
编译并执行上述代码时,会产生以下结果 -
Enter the radius of the circle
23
The circumference of the circle is 144.51
观察程序的输出语句中的格式。 变量c的格式为十进制符号后面的总位数7和2位。 Pascal允许使用数值变量进行此类输出格式化。