Kushagr Jaiswal
Kushagr Jaiswal

Reputation: 209

Data type of a value

Section 6.2.5 of the C99 Standard states that

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.

This should mean that values in C don't have data types, and that the term data type, or simply type, can be used in 2 contexts only, i.e. type of an object and type of an expression.


So, I've tried to explain the meaning of this excerpt in the form of a few examples:

  1. 5 is an expression, and the data type of this expression is int. If this expression is used as it is, then its value will be interpreted as int. But, if this expression is used as (float) 5, then its value will be interpreted as float.

  2. int x; x = 1.5;. Here, the data type of the object designated by the indentifier x is int. The data type of the expression 1.5 is double. Before assignment, the data type of the expression 1.5 gets implicitly converted to int, making the value of this expression to be interpreted as int, which then gets assigned to the object designated by x.

  3. int x = 1; double y; y = x;. Here, the data types of the objects designated by the identifiers x and y are int and double, respectively. Before assignment, the data type of the expression x gets implicitly converted to double, making the value stored in the object designated by x to be interpreted as double, which then gets assigned to the object designated by y. The data type of the object designated by x doesn't change throughout this process.

  4. int x; x = pow(2,3);. The data type of the object designated by x is int. The data type of the expression pow(2,3) is double. Before assignment, the data type of the expression pow(2,3) gets implicitly converted to int, making the value of this expression (i.e. the return value of pow(2,3)) to be interpreted as int, which then gets assigned to the object designated by x.

Is this explanation correct?

Upvotes: 0

Views: 125

Answers (1)

Lundin
Lundin

Reputation: 213306

This should mean that values in C don't have data types

It rather means that they have types. 6.2.5 describes what types are and various terms and categories related to the type system.

What might be more relevant is the concept of effective type which is applied to all expressions. The first sentence of 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.

The chapter 6.2.5 are about declared types. So in general, if you have declared a variable with a type, you must access it with an expression using that type. With a few special case exceptions further described in 6.5.


But, if this expression is used as (float) 5, then its value will be interpreted as float.

The cast triggers a conversion, which is well-defined.


2, 3 and 4.

6.2.5 doesn't have much to do with your examples. It is valid to use different arithmetic types in the same expression, as specified by simple assignment in 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.

And then further up in the same chapter:

The type of an assignment expression is the type the left operand would have after lvalue conversion.

Lvalue conversion (simplified) means that all type qualifiers of the right operand are removed and the result gets the (qualified) type of the left operand.

As for the actual conversion rules themselves (int to float etc), they are described in chapter 6.3.1, conversions with arithmetic operands.

Upvotes: 2

Related Questions