Unsigned char for int?

Assume the following:

unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar; 

Are foo and bar values โ€‹โ€‹guaranteed to be raised to int values โ€‹โ€‹to evaluate the expression foo + bar - or are implementations allowed to push them to unsigned int ?

Section 6.2.5 , paragraph 8:

For any two integer types with the same degree of correspondence and a different integer conversion (see 6.3.1.1), the range of values โ€‹โ€‹of a type with a smaller integer of the conversion rank is a subrange of values โ€‹โ€‹of another type.

Section 6.2.5 , clause 9:

If int can represent all values โ€‹โ€‹of the original type, the value is converted to int ; otherwise, it will be converted to unsigned int .

Ensuring that an integer type with a rank of a smaller integer transform has a range of values โ€‹โ€‹that is a subrange of values โ€‹โ€‹of another type seems dependent on the signature of the integer type.

  • signed char corresponds to signed int
  • unsigned char matches unsigned int

Does this mean that the value of unsigned char guaranteed only in the sub-range of unsigned int and not necessarily int ? If so, does this imply that the theoretical implementation may have an unsigned char value that is not in the int subband?

+6
source share
2 answers

Are these implementations that allow them to be promoted in unsigned int?

Implementations will be promoted by unsigned int if not all unsigned char values โ€‹โ€‹are represented in int (according to rule 6.2.5p9 in C99). The following are examples of implementation.

If so, does this imply that the theoretical implementation may have an unsigned char value that is not in the int subband?

Yes, an example: DSP cpu with CHAR_BIT 16 or 32.

For example, the TI C compiler for TMS320C55x: CHAR_BIT is 16 and UCHAR_MAX 65535, UINT_MAX 65535, but INT_MAX 32767.

http://focus.ti.com/lit/ug/spru281f/spru281f.pdf

+8
source

I came across this yesterday - I hope my answer is on the topic.

 uint8_t x = 10; uint8_t y = 250; if (x - y > 0) { // never happens } if (x - y < 0U) { // always happens } 

In my eyes, at least, it seemed that the values โ€‹โ€‹of x and y were unexpectedly advancing, when in fact it was their result, which was increased.

0
source

Source: https://habr.com/ru/post/950355/


All Articles