# stdint

***

1. **Storing Integer Data in a Portable Way:**

```c
#include <stdint.h>

int main() {
    int32_t x = 123456789;  // 32-bit integer
    uint64_t y = 123456789012345;  // 64-bit unsigned integer

    printf("x: %d\n", x);
    printf("y: %llu\n", y);

    return 0;
}
```

2. **Using Bit Masks to Extract Specific Bits:**

```c
#include <stdint.h>

int main() {
    uint8_t x = 0b11001100;

    // Extract the 3rd and 4th bits using a bit mask
    uint8_t bits = (x & 0b00011100) >> 2;

    printf("Extracted bits: %u\n", bits);

    return 0;
}
```

3. **Performing Bitwise Operations on Integers:**

```c
#include <stdint.h>

int main() {
    int32_t x = 12345;
    int32_t y = 54321;

    // Bitwise OR (|)
    int32_t or_result = x | y;

    // Bitwise AND (&)
    int32_t and_result = x & y;

    // Bitwise XOR (^)
    int32_t xor_result = x ^ y;

    printf("OR Result: %d\n", or_result);
    printf("AND Result: %d\n", and_result);
    printf("XOR Result: %d\n", xor_result);

    return 0;
}
```

4. **Working with Enums and Bit Flags:**

```c
#include <stdint.h>

enum flags {
    FLAG_A = 1 << 0,
    FLAG_B = 1 << 1,
    FLAG_C = 1 << 2
};

int main() {
    uint32_t flags_value = FLAG_A | FLAG_C;

    // Check if FLAG_B is set using bitwise AND
    if (flags_value & FLAG_B) {
        printf("FLAG_B is set\n");
    }

    return 0;
}
```

5. **Creating Custom Integer Types:**

```c
#include <stdint.h>

typedef int32_t my_int_32;
typedef uint64_t my_int_64;

int main() {
    my_int_32 x = 12345;
    my_int_64 y = 123456789012345;

    printf("x: %d\n", x);
    printf("y: %llu\n", y);

    return 0;
}
```

6. **Interfacing with System Hardware:**

```c
#include <stdint.h>

int main() {
    // Assume that the following memory address points to a hardware register
    uint8_t* register_address = (uint8_t*) 0x1000;

    // Read the value from the register
    uint8_t register_value = *register_address;

    // Write a value to the register
    *register_address = 0x55;

    return 0;
}
```

7. **Using Fixed-Width Integers in Embedded Systems:**

```c
#include <stdint.h>

int main() {
    // Define the size of data to be 2 bytes (16 bits)
    typedef uint16_t data_type;

    // Read a value of type data_type from a sensor
    data_type sensor_value = 0xABCD;

    // Send the value to another device
    uint8_t low_byte = sensor_value & 0xFF;
    uint8_t high_byte = (sensor_value >> 8) & 0xFF;
    send_low_byte(low_byte);
    send_high_byte(high_byte);

    return 0;
}
```

8. **Storing and Processing Binary Data:**

```c
#include <stdint.h>

int main() {
    uint8_t binary_data[] = {0x01, 0x02, 0x03, 0x04};
    int length = sizeof(binary_data) / sizeof(binary_data[0]);

    // Process the binary data here...

    return 0;
}
```

9. **Representing Monetary Values:**

```c
#include <stdint.h>

typedef int64_t currency_amount;

int main() {
    currency_amount balance = 1234567;

    // Convert the balance to a decimal string
    char balance_str[20];
    sprintf(balance_str, "%lld", balance);

    // Display the balance
    printf("Balance: %s\n", balance_str);

    return 0;
}
```

10. **Measuring Time Intervals Accurately:**

```c
#include <stdint.h>

int main() {
    uint64_t start_time = get_time_in_microseconds();
    // Perform some actions...
    uint64_t end_time = get_time_in_microseconds();

    uint64_t elapsed_time = end_time - start_time;

    printf("Elapsed time: %llu microseconds\n", elapsed_time);

    return 0;
}
```

11. **Storing Large Integers in a Compact Way:**

```c
#include <stdint.h>

int main() {
    uint64_t big_number = 1234567890123456789;

    // Store the big number in a file using a binary representation
    FILE* file = fopen("big_number.bin", "wb");
    fwrite(&big_number, sizeof(big_number), 1, file);
    fclose(file);

    // Read the big number back from the file
    file = fopen("big_number.bin", "rb");
    fread(&big_number, sizeof(big_number), 1, file);
    fclose(file);

    return 0;
}
```

12. **Creating Custom Data Structures:**

```c
#include <stdint.h>

typedef struct {
    uint32_t id;
    char* name;
    uint8_t age;
} person_struct;

int main() {
    person_struct person = {1, "John", 30};
    printf("ID: %u, Name: %s, Age: %u\n", person.id, person.name, person.age);
    return 0;
}
```

13. **Storing and Processing IP Addresses:**

```c
#include <stdint.h>

int main() {
    uint32_t ip_address = 0xC0A80001;  // 192.168.0.1

    // Extract the first octet (192)
    uint8_t first_octet = (ip_address >> 24) & 0xFF;

    // Convert the IP address to a string
    char ip_string[16];
    sprintf(ip_string, "%u.%u.%u.%u",
            first_octet,
            (ip_address >> 16) & 0xFF,
            (ip_address >> 8) & 0xFF,
            ip_address & 0xFF);

    printf("IP Address: %s\n", ip_string);

    return 0;
}
```

14. **Performing Fast Mathematical Operations:**

```c
#include <stdint.h>

int main() {
    uint64_t a = 123456789012345;
    uint64_t b = 543219876543210;

    // Addition
    uint64_t sum = a + b;

    // Subtraction
    uint64_t diff = a - b;

    // Multiplication
    uint64_t product = a * b;

    // Division
    uint64_t quotient = a / b;

    // Remainder
    uint64_t remainder = a % b;

    return 0;
}
```

15. **Working with Arrays of Fixed-Size Types:**

```c
#include <stdint.h>

int main() {
    uint32_t array[] = {1, 2, 3, 4, 5};
    int length = sizeof(array) / sizeof(array[0]);

    // Iterate over the array
    for (int i = 0; i < length; i++) {
        printf("Element %d: %u\n", i, array[i]);
    }

    return 0;
}
```

16. **Representing Dates and Times:**

```c
#include <stdint.h>

typedef struct {
    uint16_t year;
    uint8_t month;
    uint8_t day;
} date_struct;

typedef struct {
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
} time_struct;

int main() {
    date_struct date = {2023, 1, 1};
    time_struct time = {12, 30, 0};

    // Convert the date and time to a string
    char datetime_str[20];
    sprintf(datetime_str, "%u-%u-%u %u:%u:%u",
            date.year, date.month, date.day,
            time.hour, time.minute, time.second);

    printf("Datetime: %s\n", datetime_str);

    return 0;
}
```

17. **Storing and Processing Image Data:**

```c
#include <stdint.h>

int main() {
    uint8_t image_data[] = {0xFF, 0x00, 0xFF, 0x00, ...};
    int width = 100;
    int height = 100;

    // Process the image data here...

    return 0;
}
```

18. **Creating Custom Memory Management Functions:**

```c
#include <stdint.h>

void* my_malloc(size_t size) {
    // Allocate memory using the system's malloc function
    void* ptr = malloc(size);

    // If the allocation failed, return NULL
    if (ptr == NULL) {
        return NULL;
    }

    // Initialize the allocated memory to zero
    memset(ptr, 0, size);

    return ptr;
}

void my_free(void* ptr) {
    // Free the memory using the system's free function
    free(ptr);
}

int main() {
    // Allocate memory using my_malloc
    void* ptr = my_malloc(100);

    // Use the allocated memory...

    // Free the memory using my_free
    my_free(ptr);

    return 0;
}
```

19. **Using Atomic Operations for Concurrency:**

```c
#include <stdint.h>

int main() {
    int32_t shared_variable = 0;

    // Perform an atomic increment on shared_variable
    __sync_fetch_and_add(&shared_variable, 1);

    // Perform an atomic decrement on shared_variable
    __sync_fetch_and_sub(&shared_variable, 1);

    return 0;
}
```

20. **Storing and Processing Complex Data Structures:**

```c
#include <stdint.h>

typedef struct {
    int32_t x;
    int32_t y;
} point_struct;

int main() {
    point_struct point = {10, 20};

    // Access the x and y coordinates
    printf("x: %d, y: %d\n", point.x, point.y);

    return 0;
}
```

21. **Representing Currency Values with Maximum Precision:**

```c
#include <stdint.h>

typedef int64_t currency_amount;

int main() {
    currency_amount balance = 1234567890123456;

    // Convert the balance to a decimal string with maximum precision
    char balance_str[20];
    snprintf(balance_str, 20, "%lld", balance);

    printf("Balance: %s\n", balance_str);

    return 0;
}
```

22. **Storing and Processing Binary Images:**

```c
#include <stdint.h>

int main() {
    uint8_t image_data[] = {0x00, 0xFF, 0x00, 0xFF, ...};
    int width = 100;
    int height = 100;

    // Process the binary image data here...

    return 0;
}
```

23. **Creating Custom Data Types for Performance Optimization:**

```c
#include <stdint.h>

typedef union {
    int32_t i;
    float f;
} my_union_type;

int main() {
    my_union_type data;

    // Access the integer value
    data.i = 12345;

    // Access the float value
    data.f = 123.45;

    return 0;
}
```

24. **Generating Random Numbers with High Precision:**

```c
#include <stdint.h>

int main() {
    uint64_t random_number = random_uint64();

    printf("Random number: %llu\n", random_number);

    return 0;
}
```

25. **Storing and Processing Audio Data:**

```c
#include <stdint.h>

int main() {
    int16_t audio_data[] = {0x1234, 0x5678, 0x9ABC, ...};
    int num_samples = sizeof(audio_data) / sizeof(audio_data[0]);

    // Process the audio data here...

    return 0;
}
```

26. **Representing URLs and URIs:**

```c
#include <stdint.h>

typedef struct {
    char* scheme;
    char* host;
    uint16_t port;
    char* path;
} uri_struct;

int main() {
    uri_struct uri = {"https", "www.example.com", 443, "/index.html"};

    // Access the scheme
    printf("Scheme: %s\n", uri.scheme);

    // Construct the full URI string
    char uri_str[100];
    sprintf(uri_str, "%s://%s:%u%s",
            uri.scheme, uri.host, uri.port, uri.path);

    printf("Full URI: %s\n", uri_str);

    return 0;
}
```

27. **Storing and Processing Time Zones:**

```c
#include <stdint.h>

typedef int16_t timezone_offset;

int main() {
    timezone_offset utc_offset = -5 * 60;  // UTC-5:00

    // Convert the time zone offset to a string
    char offset_str[6];
    sprintf(offset_str, "%+03d:%02d",
            utc_offset / 60, utc_offset % 60);

    printf("Time Zone Offset: %s\n", offset_str);

    return 0;
}
```

28. **Creating Custom Bit Fields:**

```c
#include <stdint.h>

typedef struct {
    unsigned int bit_1 : 1;
    unsigned int bit_2 : 2;
    unsigned int bit_3 : 4;
} bitfield_struct;

int main() {
    bitfield_struct bitfield = {1, 2, 3};

    // Access the bit fields
    printf("Bit 1: %u\n", bitfield.bit_1);
    printf("Bit 2: %u\n", bitfield.bit_2);
    printf("Bit 3: %u\n", bitfield.bit_3);

    return 0;
}
```

29. **Storing and Processing IP Address Arrays:**

```c
#include <stdint.h>

int main() {
    uint32_t ip_addresses[] = {0xC0A80001, 0xC0A80002, 0xC0A80003};
    int num_addresses = sizeof(ip_addresses) / sizeof(ip_addresses[0]);

    // Process the IP address array here...

    return 0;
}
```

30. **Representing Geographic Coordinates:**

```c
#include <stdint.h>

typedef struct {
    double latitude;
    double longitude;
} geo_coordinate_struct;

int main() {
    geo_coordinate_struct coordinate = {37.7833, -122.4167};

    // Access the latitude and longitude
    printf("Latitude: %.6f\n", coordinate.latitude);
    printf("Longitude: %.6f\n", coordinate.longitude);

    return 0;
}
```

31. **Storing and Processing MAC Addresses:**

```c
#include <stdint.h>

int main() {
    uint8_t mac_address[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};

    // Convert the MAC address to a string
    char mac_string[18];
    sprintf(mac_string, "%02X:%02X:%02X:%02X:%02X:%02X",
            mac_address[0], mac_address[1], mac_address[2],
            mac_address[3], mac_address[4], mac_address[5]);

    printf("MAC Address: %s\n", mac_string);

    return 0;
}
```

32. **Creating Custom Enums for Error Codes:**

```c
#include <stdint.h>

enum error_codes {
    ERROR_NONE = 0,
    ERROR_INVALID_ARGUMENT = 1,
    ERROR_OUT_OF_MEMORY = 2,
    ...
};

int main() {
    error_codes error_code = ERROR_INVALID_ARGUMENT;

    // Get the error message corresponding to the error code
    const char* error_message = "Unknown error";
    switch (error_code) {
        case ERROR_NONE:
            error_message = "No error";
            break;
        case ERROR_INVALID_ARGUMENT:
            error_message = "Invalid argument";
            break;
        case ERROR_OUT_OF_MEMORY:
            error_message = "Out of memory";
            break;
        ...
    }

    printf("Error message: %s\n", error_message);

    return 0;
}
```

33. **Storing and Processing Colors in RGB Format:**

```c
#include <stdint.h>

typedef struct {
    uint8_t red;
    uint8_t green;
    uint8_t blue;
} rgb_color_struct;

int main() {
    rgb_color_struct color = {255, 0, 0};  // Red color

    // Access the red, green, and blue components
    printf("Red: %u\n", color.red);
    printf("Green: %u\n", color.green);
    printf("Blue: %u\n", color.blue);

    return 0;
}
```

34. **Representing and Converting Units of Measurement:**

```c
#include <stdint.h>

typedef enum {
    LENGTH_METERS,
    LENGTH_CENTIMETERS,
    LENGTH_INCHES,
    ...
} length_unit;

int main() {
    length_unit from_unit = LENGTH_METERS;
    length_unit to_unit = LENGTH_CENTIMETERS;
    double value = 1.5;

    // Convert the value from one unit to another
    double converted_value = convert_length(value, from_unit, to_unit);

    printf("Converted value: %.2f cm\n", converted_value);

    return 0;
}
```

35. **Storing and Processing Dates and Times in Different Formats:**

```c
#include <stdint.h>

typedef enum {
    DATE_FORMAT_YYYY_MM_DD,
    DATE_FORMAT_MM_DD_YYYY,
    DATE_FORMAT_DD_MM_YYYY,
    ...
} date_format;

typedef enum {
    TIME_FORMAT_HH_MM_SS,
    TIME_FORMAT_HH_MM_SS_AM_PM,
    TIME_FORMAT_HH_MM_SS_Z,
    ...
} time_format;

int main() {
    date_format date_format = DATE_FORMAT_YYYY_MM_DD;
    time_format time_format = TIME_FORMAT_HH_MM_SS;
    char date_str[] = "2023-01-01";
    char time_str[] = "12:30:00";

    // Convert the date and time strings to the specified formats
    char formatted_date[20];
    char formatted_time[20];
    convert_date(date_str, date_format, formatted_date);
    convert_time(time_str, time_format, formatted_time);

    printf("Formatted date: %s\n", formatted_date);
    printf("Formatted time: %s\n", formatted_time);

    return 0;
}
```

36. **Creating Custom Typedefs for Reusable Data Structures:**

```c
#include <stdint.h>

typedef struct {
    int x;
    int y;
    int z;
} point_struct;

typedef point_struct* point_t;

int main() {
    point_t p1 = malloc(sizeof(point_struct));
    p1->x = 1;
    p1->y = 2;
    p1->z = 3;

    point_t p2 = malloc(sizeof(point_struct));
    p2->x = 4;
    p2->y = 5;
    p2->z = 6;

    // Use the custom typedef to simplify the code
    ...

    free(p1);
    free(p2);

    return 0;
}
```

37. **Storing and Processing Arrays of Fixed-Size Structures:**

```c
#include <stdint.h>

typedef struct {
    int x;
    int y;
    int z;
} point_struct;

int main() {
    point_struct points[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int num_points = sizeof(points) / sizeof(points[0]);

    // Access the points
    for (int i = 0; i < num_points; i++) {
        printf("Point %d: (%d, %d, %d)\n",
                i, points[i].x, points[i].y, points[i].z);
    }

    return 0;
}
```

38. **Creating Custom Structures with Bit Fields and Padding:**

```c
#include <stdint.h>

typedef struct {
    unsigned int bit_1 : 1;
    unsigned int bit_2 : 2;
    unsigned int bit_3 : 4;
    char padding[3];  // Padding to align the next field on a 4-byte boundary
    int data;
} custom_struct;

int main() {
    custom_struct data = {1, 2, 3, 42};

    // Access the fields
    printf("Bit 1: %u\n", data.bit_1);
    printf("Bit 2: %u\n", data.bit_2);
    printf("Bit 3: %u\n", data.bit_3);
    printf("Data: %d\n", data.data);

    return 0;
}
```

39. **Creating Custom Data Types with Aligned Members:**

```c
#include <stdint.h>

typedef struct __attribute__((aligned(16))) {
    int32_t data1;
    int32_t data2;
} aligned_struct;

int main() {
    aligned_struct data = {1, 2};

    // Access the members
    printf("Data 1: %d\n", data.data1);
    printf("Data 2: %d\n", data.data2);

    return 0;
}
```

40. **Using Integer Constants for Enum Values:**

```c
#include <stdint.h>

enum colors {
    RED = 0xFF0000,
    GREEN = 0x00FF00,
    BLUE = 0x0000FF
};

int main() {
    enum colors color = RED;

    // Get the integer value of the enum
    uint32_t color_value = (uint32_t)color;

    printf("Color value: %u\n", color_value);

    return 0;
}
```

41. **Storing and Processing Text Strings in a Compact Way:**

```c
#include <stdint.h>

typedef char* string_t;

int main() {
    string_t text = "Hello, world!";

    // Get the length of the string
    int length = strlen(text);

    // Reverse the string
    for (int i = 0; i < length / 2; i++) {
        char temp = text[i];
        text[i] = text[length - i - 1];
        text[length - i - 1] = temp;
    }

    // Display the reversed string
    printf("Reversed string: %s\n", text);

    return 0;
}
```

42. **Creating Custom Bit Masks for Selective Bit Operations:**

```c
#include <stdint.h>

int main() {
    uint32_t data = 0x12345678;

    // Create a bit mask to clear bits 4 to 8
    uint32_t bit_mask = ~0x0000FF00;

    // Clear the specified bits using the bit mask
    data &= bit_mask;

    printf("Modified data: %u\n", data);

    return 0;
}
```

43. **Storing and Processing Large Amounts of Binary Data:**

```c
#include <stdint.h>

int main() {
    uint8_t* binary_data = malloc(1024);

    // Read the binary data from a file or other source
    ...

    // Process the binary data here...

    // Free the allocated memory
    free(binary_data);

    return 0;
}
```

44. **Creating Custom Functions with Variable Argument Lists:**

```c
#include <stdint.h>
#include <stdarg.h>

int sum_variable_args(int num_args, ...) {
    int sum = 0;

    // Iterate over the variable arguments
    va_list args;
    va_start(args, num_args);
    for (int i = 0; i < num_args; i++) {
        sum += va_arg(args, int);
    }
    va_end(args);

    return sum;
}

int main() {
    // Calculate the sum of 3 numbers
    int result = sum_variable_args(3, 1, 2, 3);

    printf("Sum: %d\n", result);

    return 0;
}
```

45. **Working with Hexadecimal and Octal Literals:**

```c
#include <stdint.h>

int main() {
    int value1 = 0x1234;  // Hexadecimal literal
    int value2 = 0755;  // Octal literal

    printf("Value 1: %d\n", value1);
    printf("Value 2: %d\n", value2);

    return 0;
}
```

46. **Using Integer Constants to Control Loop Iterations:**

```c
#include <stdint.h>

int main() {
    int num_iterations = 10;

    for (int i = 0; i < num_iterations; i++) {
        // Perform some actions...
    }

    return 0;
}
```

47. **Using Integer Constants to Specify Array Sizes:**

```c
#include <stdint.h>

int main() {
    const int array_size = 10;
    int array[array_size];

    for (int i = 0; i < array_size; i++) {
        array[i] = i;
    }

    return 0;
}
```

48. **Using Integer Constants in Switch Statements:**

```c
#include <stdint.h>

int main() {
    int choice = 1;

    switch (choice) {
        case 1:
            // Do something...
            break;
        case 2:
            // Do something else...
            break;
        default:
            // Handle the default case
            break;
    }

    return 0;
}
```

49. **Using Integer Constants to Initialize Global Variables:**

```c
#include <stdint.h>

int global_variable = 10;  // Initialized with an integer constant

int main() {
    // Access the global variable here...

    return 0;
}
```

50. **Using Integer Constants in Macros:**

```c
#include <stdint.h>

#define MAX_SIZE 100

int main() {
    int array[MAX_SIZE];  // Array size defined using a macro

    // Use the array here...

    return 0;
}
```
