Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit integer type conversion in C

I understand the implicit conversions of the C language between integer and floating point types, but I have a question for signed/unsigned implicit type conversions.

If you add, for example, an unsigned char and a signed int, what will be the resulting type? Would it be an unsigned int, a signed int, or something else?

I don't see anything specific in the C99 ANSI standard about this, so any help is appreciated.

like image 991
owacoder Avatar asked Feb 09 '23 03:02

owacoder


2 Answers

In C99, the reference is 6.3.1.8 "Usual arithmetic conversions".

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

  • First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.
  • Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
  • Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float. 51)
  • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
    • If both operands have the same type, then no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Addition performs the usual arithmetic conversions, so, when adding unsigned char and signed int, either:

  • first the unsigned char is promoted to int, and then both types are the same, so the result has type int, or
  • (uncommon) int cannot represent all possible unsigned char values. In this case, unsigned char is promoted to unsigned int, and the third sub-bullet applies: unsigned int has equal rank to int, so the int operand is converted to unsigned int, and the result has type unsigned int.
like image 122
Brian Bi Avatar answered Feb 11 '23 23:02

Brian Bi


It will almost certainly be a signed int and it depends on the system where the code runs. Check the paragraph Integral promotion here

unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise.

On a POSIX system for example it will definitely be a signed int as a char is always 8bits and an int is at least 16bits. Therefore an int can represent every possible value of an unsigned char. There are apparently systems where char is more that 8bits.

like image 27
Manos Nikolaidis Avatar answered Feb 11 '23 23:02

Manos Nikolaidis