Out-of-range primitive data type behavior and macros C99 PRI *

Say we have an 8-bit unsigned integer n ( UINT8_MAX=255); What is the compiler behavior for n=256? Where can I find the default behavior table when a data type value is out of range for different data types? Is there a pattern of how they behave when they are set out of range?

#include <stdio.h>
#include <inttypes.h>

uint8_t n = UINT8_MAX;
int main() {
  printf("%hhu ",n++);
  printf("%hhu",n);
  return 0;
}

Compiling with gcc -std=c99 -Wall *.c, this means:255 0

Also, is it acceptable to use C99 PRI * macros? What are their names?

+3
source share
2 answers

n=256; 256 uint8_t, n. -256, , n 0.

, , 6.3.1.3:

1 _Bool, , .

2 , , , , , type.49)

3 ; , ,

AndreyT, , , , . , 6.2.5/9:

, unsigned , , , , , .

, 3.4.3/3 :

undefined - .

(, , , , undefined).

, C . , , , , , , . . , , int, uint8_t int uint8_t . , :

uint8_t x = 100;
uint8_t y = 100;
unsigned int z = x * y;

10000, 16, , z uint8_t.

C99 PRI *? ?

? , , . GCC , , 3.4.4. 7.8.1.

C, : http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf. "" , , .

+6

.

(*) . 2 ^ N, N - . , "" ( ). 255, 256 0.

- . undefined.

(*) . , undefined ( , ). , . , undefined.

(*) undefined .

+2

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


All Articles