6.5.16.1 Simple assignment

Previous Table of Contents "New C Standard" commentary

1296 One of the following shall hold:94)

1297 —  the left operand has qualified or unqualified arithmetic type and the right has arithmetic type;

1298 —  the left operand has a qualified or unqualified version of a structure or union type compatible with the type of the right;

1299 —  both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

1300 —  one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

1301 —  the left operand is a pointer and the right is a null pointer constant;

1302 or—  the left operand has type _Bool and the right is a pointer.

1303 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.

1304 If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have qualified or unqualified versions of a compatible type;

1305 otherwise, the behavior is undefined.

1306 EXAMPLE 1 In the program fragment


        int f(void);
        char c;
        /* ... */
        if ((c = f()) == -1)
                /* ... */

the int value returned by the function may be truncated when stored in the char, and then converted back to int width prior to the comparison. In an implementation in which “plain” char has the same range of values as unsigned char (and char is narrower than int), the result of the conversion cannot be negative, so the operands of the comparison can never compare equal. Therefore, for full portability, the variable c should be declared as int.

1307 94) The asymmetric appearance of these constraints with respect to type qualifiers is due to the conversion (specified in 6.3.2.1) that changes lvalues to “the value of the expression” whichand thus removes any type qualifiers from the type category of the expression that were applied to the type category of the expression (for example, it removes const but not volatile from the type int volatile * const).

1308 EXAMPLE 2 In the fragment:


        char c;
        int i;
        long l;

l = (c = i);

the value of i is converted to the type of the assignment expression c = i, that is, char type. The value of the expression enclosed in parentheses is then converted to the type of the outer assignment expression, that is, long int type.

1309 EXAMPLE 3 Consider the fragment:


        const char **cpp;
        char *p;
        const char c = 'A';
        
        cpp = &p;       // constraint violation
        *cpp = &c;      // valid
        *p = 0;         // valid

The first assignment is unsafe because it would allow the following valid code to attempt to change the value of the const object c.

Next

Created at: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO