1
votes

I'm trying to understand the mechanics of the following conversion

#include <stdio.h>
#include <stdint.h>

int main() {
    
    int a = 0xffffffff;  
    printf("%d", a); // prints -1
    return 0;
}

According to integer constant the type of 0xffffffff is unsigned int. This can be easily checked by doing printf("%s", 0xffffffff);

Now, according to implicit conversion semantics:

"Integer promotion is the implicit conversion of a value of any integer type with rank less or equal to rank of int [...] to the value of type int or unsigned int."

and, as stated below

"the ranks of all signed integer types equal the ranks of the corresponding unsigned integer types"

so the promotion applies, because the rank of unsigned int is the same as the rank of int.

That promotion is defined as

"If int can represent the entire range of values of the original type (or the range of values of the original bit field), the value is converted to type int. Otherwise the value is converted to unsigned int"

But, and that is what I don't understand, an int cannot represent the unsigned int 4,294,967,295, but still it is converted to int. And that happens without any narrowing warning. Why is it so?

2

2 Answers

4
votes

Since the constant 0xffffffff, which (assuming int is 32 bits) has type unsigned int, is being used to initialize an object of type int, this involves a conversion from unsigned int to int.

Conversion between integer types is described in section 6.3.1.3 of the C standard:

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised

Paragraph 3 is what applies in this case. The value in question is outside the range of the destination type and the destination is signed. So an implementation-defined conversion happens.

If you compile with gcc using the -Wconversion flag, it will give you a warning:

x1.c:6:5: warning: conversion of unsigned constant value to negative integer [-Wsign-conversion]
     int a = 0xffffffff;  

Also:

This can be easily checked by doing printf("%s", 0xffffffff);

This invokes undefined behavior because the %s format specifier expects a char * which points to a null-terminated string. The value you're passing is not of this type, and likely isn't a valid memory address.

Integer promotions also don't apply here because there is no expression with a type of lower rank than int or unsigned int.

-1
votes

The thing is that you are converting (well, not you, but the compiler on behalf of you) an unsigned number (the value 0xffffffff) into a signed int which is outside of its valid range of values (you are trying to convert 4294967296 to int, but int only covers -2147483648 ... +2147483647) so you get undefined behaviour.

Depending on the compiler you use (and the architecture) you can get different values, or even an exception because of the overflow. My guess is that your compiler uses two's complement numbers, and it is just reinterpreting the number as its equivalent binary representation, which is -1 (-1 is represented internally as four bytes 0xff one after the other).