1

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?

0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

About the printf, just to clarify, I used it for verifying the type of 0xffffffff in the compiler warning. Regarding integer promotion, I can't see why it doesn't apply. I'd say I'm doing an "implicit conversion of a value of any integer type (unsigned int in this case) with rank less or equal to rank of int (unsigned int has the same rank as int) [...], to the value of type int [...]".
I thought -Wall enabled all warnings, but that's clearly not the case. Good to know :)
@perencia Certain operators perform integer promotion on their arguments. In this case you have a single constant as an initializer. Section 6.7.9p11 regarding initialization and section 6.5.16.1p2 regarding assignment only state that the right hand side is converted to the type of the left hand side. There is no mention of promotion.
@perencia Besides -Wall it's also a good idea to use -Wextra, although -Wconversion still isn't included in that.
-1

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

6 Comments

There's no undefined behaviour here
on not knowing the implementation.... it's undefined. I was in doubt when posting... but now I'm sure of my answer. Undefined implementation leads to undefined behaviour. Probably not in the sense of the standard... but in the common sense :)
@M_M, So you are the authority here, as I don't agree with you, you downvote me. Thanks, you are welcome! :) This is a very reasonable way to argument.
The C standard is the authority, not me. Comments are not for arguing either, I'm not interested in you inventing new meanings for terms which are already defined by the standard .
The C standard can be authority on C issues.... but not in language issues... The thing is that you have punished me for using the english language to describe a situation, based on your knowledge of the C standard. I know perfectly the difference between the meanings in the standard and the english language. In this case I should have said implementation defined, but the actual problem is that the implementation is undefined, so undefined behaviour is perfectly legal. Or perhaps do you know what is the exact behaviour that is expected in this case? IMHO the PO won't understand...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.