C Fixed-Width Integers
In this page:
#include <stdint.h>
int32_t variable_name = value; // also int8_t int16_t int64_t
uint32_t variable_name = value; // also uint8_t uint16_t uint64_t
The Portability Problem
Plain types like int and long don't have a guaranteed size in the C standard -- int is commonly 32 bits today but was 16 bits on older systems, so code that assumes a specific width can silently misbehave when compiled on a different platform.
Example: The Portability Problem
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("int size here: %zu bytes", sizeof(int));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Signed Fixed-Width Types
<stdint.h> defines int8_t, int16_t, int32_t, and int64_t, each guaranteed to be exactly that many bits wide on every conforming platform, which is exactly what protocol formats, file formats, and hardware registers need.
Example: Signed Fixed-Width Types
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
// Declare id and set it to 100000
int32_t id = 100000;
// Print formatted text to the screen
printf("%d", id);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unsigned Fixed-Width Types
The unsigned counterparts uint8_t, uint16_t, uint32_t, and uint64_t follow the same naming pattern and are the standard choice for raw byte buffers, bit flags, and anything that should never represent a negative value.
Example: Unsigned Fixed-Width Types
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
// Declare flags and set it to 255
uint8_t flags = 255;
// Print formatted text to the screen
printf("%u", flags);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Fixed-Width Values
Because the underlying type behind int32_t can vary by platform, printf format specifiers like %d aren't strictly portable for it -- <inttypes.h> provides macros like PRId32 and PRIu64 that expand to the correct specifier for each fixed-width type.
Example: Printing Fixed-Width Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
#include <inttypes.h>
// Program execution starts in main()
int main() {
// Declare id and set it to 42
int32_t id = 42;
// Print formatted text to the screen
printf("%" PRId32, id);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
When to Use Fixed-Width Types
Reach for fixed-width types when exact size matters -- binary file formats, network packets, embedded registers -- but for everyday counters and loop variables, plain int remains simpler and is often what the platform's CPU handles most efficiently.
Example: When to Use Fixed-Width Types
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
// Declare filePacketSize and set it to 1024
int32_t filePacketSize = 1024;
// Declare loopCounter and set it to 0
int loopCounter = 0;
// Print formatted text to the screen
printf("%d %d", filePacketSize, loopCounter);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
intand assuming it is 32 bits everywhere, whenint32_tguarantees the width. - Forgetting
#include <stdint.h>before usingint32_toruint8_t. - Printing a fixed-width value with
%dinstead of the macros from<inttypes.h>such asPRId64.
- This chapter introduces C, its history, how to set up a compiler, and the structure of a first program.
- You learn syntax, statements, comments, keywords, and identifiers, which form the basic rules of every C program.
- It also covers data types, variables, constants, type conversion, and
sizeof, which control how values are stored.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers