← Back to C Course | Chapter 1: Introduction & Basics | Lesson 21 of 21

C Fixed-Width Integers

Fixed-width integers are whole-number types with an exact size, like a jar that always holds exactly the same amount on any computer. They make programs behave the same everywhere.
Syntax
c
#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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using int and assuming it is 32 bits everywhere, when int32_t guarantees the width.
  2. Forgetting #include <stdint.h> before using int32_t or uint8_t.
  3. Printing a fixed-width value with %d instead of the macros from <inttypes.h> such as PRId64.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.