值的数据类型
Data type of a value
C99 标准的第 6.2.5
节指出
The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it.
这应该意味着 C 中的 values
没有数据类型,术语 data type
或简称 type
只能在 2 个上下文中使用,即object
的类型和 expression
.
的类型
因此,我尝试以几个示例的形式来解释这段摘录的含义:
5
是一个表达式,这个表达式的数据类型是int
。如果按原样使用此表达式,则其值将被解释为 int
。但是,如果此表达式用作 (float) 5
,则其值将被解释为 float
.
int x; x = 1.5;
。这里,标识符x
指定的对象的数据类型是int
。表达式 1.5
的数据类型是 double
。在赋值之前,表达式 1.5
的数据类型被隐式转换为 int
,使该表达式的值被解释为 int
,然后将其赋值给由 x
.
int x = 1; double y; y = x;
。这里,标识符x
和y
所指定的对象的数据类型分别为int
和double
。在赋值之前,表达式 x
的数据类型被隐式转换为 double
,使得存储在由 x
指定的对象中的值被解释为 double
,然后被分配给 y
指定的对象。 x
指定的对象的数据类型在整个过程中不会改变。
int x; x = pow(2,3);
。 x
指定的对象的数据类型是int
。表达式 pow(2,3)
的数据类型是 double
。在赋值之前,表达式 pow(2,3)
的数据类型被隐式转换为 int
,使得该表达式的值(即 pow(2,3)
的 return 值)被解释为int
,然后分配给 x
指定的对象。
这个解释正确吗?
This should mean that values in C don't have data types
这更意味着它们有类型。 6.2.5 描述了什么是类型以及与类型系统相关的各种术语和类别。
可能更相关的是适用于所有表达式的有效类型的概念。 6.5/6第一句:
The effective type of an object for an access to its stored value is the declared type of the object, if any.
第 6.2.5 章是关于声明类型的。所以一般来说,如果您声明了一个具有类型的变量,则必须使用该类型的表达式访问它。除了 6.5 中进一步描述的一些特殊情况例外。
But, if this expression is used as (float) 5, then its value will be interpreted as float.
转换触发转换,定义明确。
2、3 和 4。
6.2.5 与您的示例没有太大关系。在同一个表达式中使用不同的算术类型是有效的,如 6.5.16 中简单赋值的规定。
One of the following shall hold:
— the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;
/--/
In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.
然后在同一章中更上一层:
The type of an assignment expression is the type the left operand would have
after lvalue conversion.
左值转换(简化)意味着删除右操作数的所有类型限定符,结果获得左操作数的(限定)类型。
至于实际的转换规则本身(int 到 float 等),它们在第 6.3.1 章中有描述,使用算术操作数进行转换。
C99 标准的第 6.2.5
节指出
The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it.
这应该意味着 C 中的 values
没有数据类型,术语 data type
或简称 type
只能在 2 个上下文中使用,即object
的类型和 expression
.
因此,我尝试以几个示例的形式来解释这段摘录的含义:
5
是一个表达式,这个表达式的数据类型是int
。如果按原样使用此表达式,则其值将被解释为int
。但是,如果此表达式用作(float) 5
,则其值将被解释为float
.int x; x = 1.5;
。这里,标识符x
指定的对象的数据类型是int
。表达式1.5
的数据类型是double
。在赋值之前,表达式1.5
的数据类型被隐式转换为int
,使该表达式的值被解释为int
,然后将其赋值给由x
.int x = 1; double y; y = x;
。这里,标识符x
和y
所指定的对象的数据类型分别为int
和double
。在赋值之前,表达式x
的数据类型被隐式转换为double
,使得存储在由x
指定的对象中的值被解释为double
,然后被分配给y
指定的对象。x
指定的对象的数据类型在整个过程中不会改变。int x; x = pow(2,3);
。x
指定的对象的数据类型是int
。表达式pow(2,3)
的数据类型是double
。在赋值之前,表达式pow(2,3)
的数据类型被隐式转换为int
,使得该表达式的值(即pow(2,3)
的 return 值)被解释为int
,然后分配给x
指定的对象。
这个解释正确吗?
This should mean that values in C don't have data types
这更意味着它们有类型。 6.2.5 描述了什么是类型以及与类型系统相关的各种术语和类别。
可能更相关的是适用于所有表达式的有效类型的概念。 6.5/6第一句:
The effective type of an object for an access to its stored value is the declared type of the object, if any.
第 6.2.5 章是关于声明类型的。所以一般来说,如果您声明了一个具有类型的变量,则必须使用该类型的表达式访问它。除了 6.5 中进一步描述的一些特殊情况例外。
But, if this expression is used as (float) 5, then its value will be interpreted as float.
转换触发转换,定义明确。
2、3 和 4。
6.2.5 与您的示例没有太大关系。在同一个表达式中使用不同的算术类型是有效的,如 6.5.16 中简单赋值的规定。
One of the following shall hold:
— the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;
/--/
In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.
然后在同一章中更上一层:
The type of an assignment expression is the type the left operand would have after lvalue conversion.
左值转换(简化)意味着删除右操作数的所有类型限定符,结果获得左操作数的(限定)类型。
至于实际的转换规则本身(int 到 float 等),它们在第 6.3.1 章中有描述,使用算术操作数进行转换。