02. Primitive Data Types & Fixed-Width Integers
🟢 Difficulty: Beginner | ISO C Primitives, Format Specifiers & stdint.h
📖 Definition
A data type in C specifies the set of values a variable can hold, the bit layout used to represent those values in memory, and the hardware operations that can be performed on them. C provides built-in primitive types (integers, floating-point numbers, characters, booleans) and standard fixed-width integer types via <stdint.h>.
🎯 Why It Is Used
Different computational tasks require different precision and memory constraints. Choosing appropriate data types enables:
- Memory Footprint Optimization: Using 1-byte
uint8_tfor sensor readings saves 75% RAM compared to 4-byteint. - Cross-Platform Portability: Standard C integer sizes vary across compilers;
<stdint.h>guarantees exact bit counts regardless of OS. - Numerical Precision: IEEE 754
doubleprevents floating-point rounding errors in financial and scientific calculations.
⚙️ How It Works
In ISO C, data types specify minimum bit width guarantees, not fixed absolute sizes. Compilers map these primitives to target CPU registers:
char: Exactly 1 byte (8 bits) by definition in C. Signedness is implementation-defined.int: At least 16 bits (typically 32 bits / 4 bytes on modern x86_64 / ARM64).float: Typically 32-bit single-precision IEEE 754 floating point.double: Typically 64-bit double-precision IEEE 754 floating point.
📝 Syntax & Format Specifiers Table
| C Data Type | C Header Needed | Format Specifier | Typical Size | Value Range |
|---|---|---|---|---|
char | Standard | %c or %d | 1 Byte | -128 to +127 (or 0 to 255) |
int | Standard | %d or %i | 4 Bytes | -2,147,483,648 to +2,147,483,647 |
unsigned int | Standard | %u | 4 Bytes | 0 to 4,294,967,295 |
long long | Standard | %lld | 8 Bytes | -9 × 1018 to +9 × 1018 |
float | Standard | %f | 4 Bytes | 6–7 decimal digits precision |
double | Standard | %lf | 8 Bytes | 15–17 decimal digits precision |
bool | <stdbool.h> | %d | 1 Byte | true (1) or false (0) |
int32_t | <stdint.h> | PRId32 or %d | Exact 4 Bytes | -2,147,483,648 to +2,147,483,647 |
uint64_t | <stdint.h> | PRIu64 or %llu | Exact 8 Bytes | 0 to 18,446,744,073,709,551,615 |
size_t | <stddef.h> / <stdio.h> | %zu | 4 or 8 Bytes | Unsigned integer for byte sizes & indices |
🔀 Standard vs Fixed-Width Integer Types
When writing portable systems code, standard C types (like long) change size between Operating Systems (e.g. long is 4 bytes on 64-bit Windows, but 8 bytes on 64-bit Linux). <stdint.h> solves this by defining exact-width types:
int8_t/uint8_t: Exactly 8 bits (1 byte).int16_t/uint16_t: Exactly 16 bits (2 bytes).int32_t/uint32_t: Exactly 32 bits (4 bytes).int64_t/uint64_t: Exactly 64 bits (8 bytes).
🌱 Beginner Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int item_count = 12;
double price_per_unit = 24.99;
char currency = '$';
bool in_stock = true;
printf("Items: %d\n", item_count);
printf("Total Cost: %c%.2lf\n", currency, item_count * price_per_unit);
printf("In Stock Status: %d\n", in_stock);
return 0;
}
🌼 Intermediate Example: Fixed-Width Integers & Inttypes Macros
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main(void) {
uint8_t sensor_status = 0xFF; // 8-bit mask (255)
uint32_t packets_sent = 1000000;
uint64_t total_network_bytes = 987654321012345ULL;
printf("Sensor Status Mask : 0x%" PRIx8 " (%u)\n", sensor_status, sensor_status);
printf("Packets Sent : %" PRIu32 "\n", packets_sent);
printf("Network Bytes Total: %" PRIu64 " bytes\n", total_network_bytes);
return 0;
}
🌲 Advanced Example: Precision Loss & Floating-Point IEEE 754 Inspection
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main(void) {
printf("=== Floating Point IEEE 754 Equality Demo ===\n");
double a = 0.1;
double b = 0.2;
double sum = a + b;
printf("0.1 + 0.2 formatted to 17 decimal places: %.17lf\n", sum);
// Naive equality comparison fails in floating point arithmetic!
if (sum == 0.3) {
printf("Exact Match! (Rare)\n");
} else {
printf("Warning: Floating Point Rounding Error Detected! (sum != 0.3)\n");
}
// Safe Epsilon Floating Point Comparison Pattern
double epsilon = 1e-9;
if (fabs(sum - 0.3) < epsilon) {
printf("Safe Epsilon Comparison: Values are equal within %e threshold!\n", epsilon);
}
return 0;
}
🔍 Line-by-Line Explanation
#include <stdint.h>: Defines exact-width types likeuint64_t.#include <inttypes.h>: Provides portable format specifier macros likePRIu64andPRIx8.double sum = a + b;: In IEEE 754, binary floating point cannot represent 0.1 perfectly, resulting in0.30000000000000004.fabs(sum - 0.3) < epsilon: Standard C pattern for comparing floating-point numbers without precision bugs.
💻 Expected Terminal Output
=== Floating Point IEEE 754 Equality Demo ===
0.1 + 0.2 formatted to 17 decimal places: 0.30000000000000004
Warning: Floating Point Rounding Error Detected! (sum != 0.3)
Safe Epsilon Comparison: Values are equal within 1.000000e-09 threshold!
⚠️ Common Mistakes & Pitfalls
- Using Direct
==on Floats: Never useif (float_var == 0.3)due to IEEE 754 representation inaccuracies. Always use an epsilon threshold. - Wrong Specifier for
size_t: Using%dor%uforsize_tcauses compiler warnings on 64-bit platforms. Always use%zu. - Integer Overflow: Storing values greater than
2,147,483,647in a standard signed 32-bitinttriggers signed integer overflow (Undefined Behavior).
📌 Important Rules & Limitations
- Signed integer overflow is Undefined Behavior in C. Unsigned integer overflow wraps around predictably using modulo 2N.
- To use
bool,true, andfalsein C99/C11, you must#include <stdbool.h>.
🌐 Multilingual Explanation: English
C provides primitive types (int, float, double, char) whose byte sizes vary by architecture. For portable code, <stdint.h> provides exact fixed-width integer types like int32_t and uint64_t.
🌐 Multilingual Explanation: Hindi (Roman Script)
C mein basic data types (int, float, double, char) ke sizes compiler aur CPU par depend karte hain. Standard cross-platform code ke liye hum <stdint.h> ke fixed-width types (jaise uint32_t, uint64_t) use karte hain.
🌐 Multilingual Explanation: Marathi (Roman Script)
C madhye basic data types che memory size compiler nusar badalutatat. Code saglede chaltavava mhanun <stdint.h> madhil fixed-width integer types (jasakhi int32_t, uint64_t) vaparane recommended aste.
❓ Practice Questions
- Why is
<stdint.h>preferred over standardlongin cross-platform systems programming? - What is the correct format specifier for a
size_tvariable? - Why does
0.1 + 0.2 == 0.3evaluate to false in C?
🚀 Practical Coding Challenge
Write a C program named type_limits.c using <stdint.h> and <limits.h> that prints the minimum and maximum printable values of INT8_MAX, UINT8_MAX, INT32_MIN, and UINT64_MAX.
🧭 Navigation & Breadcrumbs
← Previous: 01. Variable Declarations | Module 03 Guide | Next: 03. Constants & Qualifiers →