C inttypes.h Header
Integer type formatting and conversion macros
🔢 What is inttypes.h?
The inttypes.h header provides format macros for printf/scanf with fixed-width integer types and functions for converting strings to integers across different platforms.
#include <inttypes.h>
#include <stdio.h>
int main() {
int64_t big_num = 1234567890123LL;
printf("Big number: %" PRId64 "\n", big_num);
return 0;
}
Output:
Key inttypes.h Features
Format Macros
Portable printf/scanf format specifiers
printf("%" PRId32 "\n", value);
scanf("%" SCNd64, &big_val);
String Conversion
Convert strings to integer types safely
intmax_t num = strtoimax(str, NULL, 10);
uintmax_t unum = strtoumax(str, NULL, 16);
Fixed Width Types
Work with exact-width integer types
int32_t exact32 = 42;
uint64_t exact64 = 1000ULL;
Cross Platform
Portable code across different systems
#include <inttypes.h>
// Works on all platforms
🔹 Printf Format Macros
Portable format specifiers from inttypes.h ensure correct printing of fixed-width integer types across different platforms and architectures. Use macros like PRId32, PRIu64, and PRIx16 for type-safe integer output without platform-specific assumptions. For example, printf("%" PRId32, value) correctly prints 32-bit integers. These macros prevent formatting errors that occur when hardcoding format specifiers on different systems. Professional portable code relies on these standard macros. Understanding inttypes ensures your code works reliably across diverse computing environments.
#include <inttypes.h>
#include <stdio.h>
int main() {
int8_t small = 127;
int32_t medium = 2147483647;
int64_t large = 9223372036854775807LL;
printf("8-bit: %" PRId8 "\n", small);
printf("32-bit: %" PRId32 "\n", medium);
printf("64-bit: %" PRId64 "\n", large);
// Hexadecimal format
printf("Hex: %" PRIx32 "\n", medium);
return 0;
}
Output:
32-bit: 2147483647
64-bit: 9223372036854775807
Hex: 7fffffff
🔹 String to Integer Conversion
Converting strings to various integer types with error checking enables safe parsing of user input and external data in C applications. Functions like strtol(), strtoll(), and strtoumax() provide robust conversion with extensive validation. Check the endptr parameter to verify complete conversion and examine errno for overflow conditions. Use inttypes macros for format consistency. Proper string conversion prevents crashes from invalid input and handles edge cases like range overflow. Professional input parsing implements comprehensive validation.
#include <inttypes.h>
#include <stdio.h>
int main() {
char *str1 = "12345";
char *str2 = "ABCD";
char *endptr;
// Convert decimal string
intmax_t num1 = strtoimax(str1, &endptr, 10);
printf("Decimal: %" PRIdMAX "\n", num1);
// Convert hexadecimal string
uintmax_t num2 = strtoumax(str2, &endptr, 16);
printf("Hex: %" PRIuMAX "\n", num2);
return 0;
}
Output:
Hex: 43981
🔹 Scanf Format Macros
Reading integers safely with scanf format macros ensures proper parsing of different integer types matching their actual sizes on current platforms. Use macros like SCNd32, SCNu64, and SCNx16 for platform-independent integer input operations. Example: scanf("%" SCNd32, &value) correctly reads 32-bit integers. These macros prevent format mismatches causing data corruption or crashes. They demonstrate the importance of portable programming practices. Using scanf with inttypes macros ensures consistent and reliable integer input across all systems.
#include <inttypes.h>
#include <stdio.h>
int main() {
int32_t value32;
int64_t value64;
printf("Enter a 32-bit number: ");
scanf("%" SCNd32, &value32);
printf("Enter a 64-bit number: ");
scanf("%" SCNd64, &value64);
printf("You entered: %" PRId32 " and %" PRId64 "\n",
value32, value64);
return 0;
}
Sample Input/Output:
Enter a 64-bit number: 1000000
You entered: 42 and 1000000