Operacje arytmetyczne na typach całkowitych

0

Witam
W ostatnim czasie ktoś zadał mi pytanie czy taki kod zadziała poprawnie dla a = 255 i b = 255?

int add(unsigned char a, unsigned char b)
{ return a+b; }

mniejsza z tym czy odpowiedziałem poprawnie czy nie, ponieważ w chwili obecnej chodzi mi bardziej o logikę stojącą za tym działaniem. Czytam więc standard języka C i się zastanawiam na jakiej podstawie to działa poprawnie czy jest to coś w sylu
int c = a+b;
gdzie c to wartość zwracana z funkcji czy to na podstawie tego, że jak mi się wydaje i o czym mówi dokument każdy typ całkowity o rozmiarze mniejszym od int w operacjach arytmetycznych jest konwertowany na int? Tego akurat nie jestem do końca pewien bo tego mój mały rozumek do końca nie ogarnia na podstawie tekstu. ;-)
Jest mi to ktoś w stanie prosto wytłumaczyć?

Czy to w ogóle wynika z tego fragmentu poniżej czy z czegoś zupełnie innego?

6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers
1 Every integer type has an integer conversion rank defined as follows:
— No two signed integer types shall have the same rank, even if they hav e the same
representation.
— The rank of a signed integer type shall be greater than the rank of any signed integer
type with less precision.
— The rank of long long int shall be greater than the rank of long int, which
shall be greater than the rank of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed char.
— The rank of any unsigned integer type shall equal the rank of the corresponding
signed integer type, if any.
— The rank of any standard integer type shall be greater than the rank of any extended
integer type with the same width.
— The rank of char shall equal the rank of signed char and unsigned char.
— The rank of _Bool shall be less than the rank of all other standard integer types.
— The rank of any enumerated type shall equal the rank of the compatible integer type
(see 6.7.2.2).
— The rank of any extended signed integer type relative to another extended signed
integer type with the same precision is implementation-defined, but still subject to the
other rules for determining the integer conversion rank.
— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has
greater rank than T3, then T1 has greater rank than T3.
2 The following may be used in an expression wherever an int or unsigned int may
be used:
42 Language §6.3.1.1
WG14/N1124 Committee Draft — May 6, 2005 ISO/IEC 9899:TC2
— An object or expression with an integer type whose integer conversion rank is less
than or equal to the rank of int and unsigned int.
— A bit-field of type _Bool, int, signed int, or unsigned int.
If an int can represent all values of the original type, the value is converted to an int;
otherwise, it is converted to an unsigned int. These are called the integer
promotions. 48) All other types are unchanged by the integer promotions.

0

Do obliczeń typy całkowite mniejsze od naturalnej dla danej platformy dostają promocje - do int.

1 użytkowników online, w tym zalogowanych: 0, gości: 1