# inttypes

***

**1. Getting Byte Order of a Value**

```c
#include <inttypes.h>
uint64_t value = 0x123456789ABCDEF;
printf("Big-endian: %" PRIx64 "\n", value);
printf("Little-endian: %" PRIx64 "\n", __bswap_64(value));
```

**2. Printing Integer Values in Hexadecimal**

```c
#include <inttypes.h>
int16_t number = 0xABCD;
printf("Hexadecimal: %" PRIx16 "\n", number);
```

**3. Converting Strings to Integers**

```c
#include <inttypes.h>
const char *str = "1234";
int64_t value;
sscanf(str, "%" SCNd64, &value);
printf("Value: %" PRId64 "\n", value);
```

**4. Calculating Minimum and Maximum Values**

```c
#include <inttypes.h>
intmax_t min = INTMAX_MIN;
intmax_t max = INTMAX_MAX;
printf("Minimum value: %" PRIdMAX "\n", min);
printf("Maximum value: %" PRIdMAX "\n", max);
```

**5. Verifying Integer Overflow**

```c
#include <inttypes.h>
int64_t a = INT64_MAX;
int64_t b = 2;
if (__builtin_add_overflow(a, b, &a)) {
  printf("Integer overflow occurred!\n");
}
```

**6. Converting Integers to Strings**

```c
#include <inttypes.h>
int64_t number = 123456789;
char buffer[20];
snprintf(buffer, sizeof(buffer), "%" PRId64, number);
printf("String: %s\n", buffer);
```

**7. Parsing Binary Numbers from Strings**

```c
#include <inttypes.h>
const char *str = "10101101";
uint8_t number;
number = strtol(str, NULL, 2);
printf("Binary number: %02x\n", number);
```

**8. Printing Float Values in Hexadecimal**

```c
#include <inttypes.h>
float value = 12345.6789;
long double hexValue;
hexValue = *(int64_t *)&value;
printf("Hexadecimal: %" PRIx64 "\n", hexValue);
```

**9. Generating Random Integers**

```c
#include <inttypes.h>
uint32_t randomValue;
randomValue = rand() % (UINT32_MAX - 1) + 1;
printf("Random value: %" PRIu32 "\n", randomValue);
```

**10. Bitwise Operations with Integer Types**

```c
#include <inttypes.h>
uint64_t a = 0xABCDEF12;
uint64_t b = 0x34567890;
printf("Bitwise AND: %" PRIx64 "\n", a & b);
printf("Bitwise OR: %" PRIx64 "\n", a | b);
printf("Bitwise XOR: %" PRIx64 "\n", a ^ b);
```

**11. Shifting Integers**

```c
#include <inttypes.h>
int64_t value = 0x123456789ABCDEF;
printf("Shifted left by 4: %" PRIx64 "\n", value << 4);
printf("Shifted right by 8: %" PRIx64 "\n", value >> 8);
```

**12. Using Macros to Simplify Integer Operations**

```c
#include <inttypes.h>
intmax_t value = INTMAX_MAX;
printf("Negative value: %" PRIiMAX "\n", INTMAX_MIN);
```

**13. Defining Custom Integer Types**

```c
#include <inttypes.h>
typedef int32_t my_int32_t;
my_int32_t value = 123456789;
printf("Value: %" PRId32 "\n", value);
```

**14. Using Integer Types in Unions**

```c
#include <inttypes.h>
union {
  int64_t value;
  uint32_t parts[2];
} data;
```

**15. Printing Integer Values with Leading Zeros**

```c
#include <inttypes.h>
int32_t value = 123;
printf("Leading zeros: %08" PRId32 "\n", value);
```

**16. Converting Integers to Characters**

```c
#include <inttypes.h>
uint8_t character = 'A';
printf("Character: %" PRIu8 "\n", character);
```

**17. Using Integer Types in Masks**

```c
#include <inttypes.h>
int64_t mask = 0xFFFFFFFFFFFFFFFF;
int64_t value = 0x123456789ABCDEF;
printf("Masked value: %" PRIx64 "\n", value & mask);
```

**18. Incrementing and Decrementing Integer Values**

```c
#include <inttypes.h>
int64_t value = 123456789;
value++;
printf("Incremented value: %" PRId64 "\n", value);
```

**19. Comparing Integer Values**

```c
#include <inttypes.h>
intmax_t a = 123456789;
intmax_t b = 987654321;
if (a == b) {
  printf("Values are equal\n");
}
```

**20. Reversing the Order of Integer Bytes**

```c
#include <inttypes.h>
uint64_t value = 0x123456789ABCDEF;
value = __bswap_64(value);
printf("Reversed value: %" PRIx64 "\n", value);
```

**21. Calculating the Size of Integer Types**

```c
#include <inttypes.h>
printf("Size of int8_t: %zu bytes\n", sizeof(int8_t));
printf("Size of uint64_t: %zu bytes\n", sizeof(uint64_t));
```

**22. Using Integer Types in Array Indices**

```c
#include <inttypes.h>
int64_t array[10];
array[5] = 123456789;
printf("Array value: %" PRId64 "\n", array[5]);
```

**23. Passing Integer Types as Function Arguments**

```c
#include <inttypes.h>
void function(int64_t value) {
  printf("Function argument: %" PRId64 "\n", value);
}
```

**24. Using Integer Types in Return Values**

```c
#include <inttypes.h>
int64_t function() {
  return 123456789;
}
```

**25. Storing Integer Values in Files**

```c
#include <inttypes.h>
FILE *file = fopen("data.bin", "wb");
int64_t value = 123456789;
fwrite(&value, sizeof(value), 1, file);
fclose(file);
```

**26. Reading Integer Values from Files**

```c
#include <inttypes.h>
FILE *file = fopen("data.bin", "rb");
int64_t value;
fread(&value, sizeof(value), 1, file);
fclose(file);
```

**27. Using Integer Types in Sockets**

```c
#include <inttypes.h>
#include <sys/socket.h>
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_port = htons(1234);
bind(sock, (struct sockaddr *)&addr, sizeof(addr));
```

**28. Using Integer Types in Threads**

```c
#include <inttypes.h>
#include <pthread.h>
pthread_t thread;
pthread_create(&thread, NULL, function, (void *)123456789);
```

**29. Using Integer Types in Signal Handling**

```c
#include <inttypes.h>
#include <signal.h>
void handler(int signo) {
  printf("Signal number: %" PRId32 "\n", signo);
}
```

**30. Using Integer Types in Interrupts**

```c
#include <inttypes.h>
#include <interrupt.h>
void interrupt_handler(int vector) {
  printf("Interrupt vector: %" PRIu32 "\n", vector);
}
```

**31. Using Integer Types in I2C Communication**

```c
#include <inttypes.h>
#include <i2c.h>
uint8_t data = 0x12;
i2c_write(I2C_PORT, 0x77, &data, 1);
```

**32. Using Integer Types in SPI Communication**

```c
#include <inttypes.h>
#include <spi.h>
uint8_t txData = 0x12;
uint8_t rxData;
spi_transfer(SPI_PORT, &txData, &rxData, 1);
```

**33. Using Integer Types in UART Communication**

```c
#include <inttypes.h>
#include <uart.h>
const char *str = "Hello world!\n";
uart_write(UART_PORT, str, strlen(str));
```

**34. Using Integer Types in CAN Communication**

```c
#include <inttypes.h>
#include <can.h>
can_message_t message;
message.id = 0x123;
message.data[0] = 0x45;
message.data[1] = 0x67;
can_send(CAN_PORT, &message);
```

**35. Using Integer Types in USB Communication**

```c
#include <inttypes.h>
#include <usb.h>
usb_device_t device;
usb_open(&device);
uint8_t data[64];
usb_read(&device, data, 64);
```

**36. Using Integer Types in Network Communication**

```c
#include <inttypes.h>
#include <net.h>
net_socket_t socket;
net_socket_create(&socket, NET_SOCKET_TCP);
net_socket_connect(&socket, "192.168.1.1", 80);
```

**37. Using Integer Types in Graphics**

```c
#include <inttypes.h>
#include <graphics.h>
canvas_t canvas;
color_t color = 0xFF0000;
canvas_set_pixel(&canvas, 100, 100, color);
```

**38. Using Integer Types in Audio**

```c
#include <inttypes.h>
#include <audio.h>
sound_buffer_t buffer;
sound_play(&buffer);
```

**39. Using Integer Types in Video**

```c
#include <inttypes.h>
#include <video.h>
video_stream_t stream;
video_stream_play(&stream);
```

**40. Using Integer Types in Image Processing**

```c
#include <inttypes.h>
#include <image.h>
image_t image;
image_load(&image, "image.png");
```

**41. Using Integer Types in Data Structures**

```c
#include <inttypes.h>
#include <list.h>
list_t list;
list_add(&list, (void *)123456789);
```

**42. Using Integer Types in Hash Tables**

```c
#include <inttypes.h>
#include <hashtable.h>
hashtable_t hashtable;
hashtable_insert(&hashtable, "key", (void *)123456789);
```

**43. Using Integer Types in Trees**

```c
#include <inttypes.h>
#include <tree.h>
tree_t tree;
tree_insert(&tree, 123456789);
```

**44. Using Integer Types in Graphs**

```c
#include <inttypes.h>
#include <graph.h>
graph_t graph;
graph_add_vertex(&graph, 123456789);
```

**45. Using Integer Types in Machine Learning**

```c
#include <inttypes.h>
#include <model.h>
model_t model;
model_train(&model, data);
```

**46. Using Integer Types in Numerical Analysis**

```c
#include <inttypes.h>
#include <math.h>
double value = 123456789.123456789;
printf("Sine of value: %f\n", sin(value));
```

**47. Using Integer Types in Data Analysis**

```c
#include <inttypes.h>
#include <stat.h>
stat_t stat;
stat_calculate(&stat, data);
```

**48. Using Integer Types in Scientific Computing**

```c
#include <inttypes.h>
#include <sim.h>
sim_t sim;
sim_run(&sim, data);
```

**49. Using Integer Types in Game Development**

```c
#include <inttypes.h>
#include <game.h>
game_t game;
game_run(&game);
```

**50. Using Integer Types in Medical Imaging**

```c
#include <inttypes.h>
#include <imaging.h>
image_t image;
image_load(&image, "scan.dcm");
```
