Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The int keyword in C designates the fundamental signed integer data type. It represents whole numbers without a fractional component and is architecturally mapped to the natural word size of the target machine’s execution environment, making it the primary type for standard arithmetic operations.
int uninitialized_var;
int initialized_var = 42;
signed int explicit_signed = -10; // 'signed' is implicit in 'int'

Memory Size and Architecture Dependence

Unlike fixed-width types (e.g., int32_t), the exact byte size of an int is compiler- and architecture-dependent. The ISO C Standard mandates only that an int must be capable of holding a minimum range of values, requiring at least 16 bits (2 bytes) of storage.
  • 16-bit architectures: int is typically 2 bytes.
  • 32-bit (ILP32) and 64-bit (LP64/LLP64) architectures: int is universally implemented as 4 bytes (32 bits).
You can determine the exact size on a specific platform using the sizeof operator, which evaluates to a size_t constant expression:
#include <stddef.h>

size_t int_size = sizeof(int);

Value Range and Limits

Because int is signed by default, one bit is reserved for the sign. The values are represented in memory using Two’s Complement binary encoding. The absolute limits of the int type for a given compiler are defined as macros in the <limits.h> standard library header.
  • 16-bit implementation:
    • Minimum (INT_MIN): -32,768 (215-2^{15})
    • Maximum (INT_MAX): 32,767 (21512^{15} - 1)
  • 32-bit implementation:
    • Minimum (INT_MIN): -2,147,483,648 (231-2^{31})
    • Maximum (INT_MAX): 2,147,483,647 (23112^{31} - 1)

Integer Promotion

A fundamental semantic of C is that int serves as the baseline type for arithmetic evaluation. Under the rules of Integer Promotion, any integer type smaller than int (such as char and short) is automatically converted to int before arithmetic operations, bitwise operations, or logical evaluations are performed. If an int cannot represent all possible values of the original type, it is promoted to unsigned int instead.
void demonstrate_promotion(void) {
    char a = 100;
    char b = 50;
    
    // 'a' and 'b' are implicitly promoted to 'int' before addition.
    // The result of (a + b) is of type 'int'.
    int result = a + b; 
}

Signed Integer Overflow

In C, overflowing a signed integer—such as exceeding INT_MAX or falling below INT_MIN—results in Undefined Behavior (UB). The compiler is permitted to optimize out overflow checks or assume overflow never occurs, which can lead to unpredictable program execution. This strictly contrasts with unsigned int, which safely wraps around using modulo arithmetic.
#include <limits.h>

void trigger_overflow(void) {
    int max_val = INT_MAX;
    int overflowed = max_val + 1; // UNDEFINED BEHAVIOR
}

Type Modifiers

The int base type can be altered using sign and size modifiers to adjust its memory footprint and value range. When a modifier is used, the int keyword itself becomes optional.
unsigned int u_val = 4000000000U; // Drops the sign bit to double the positive range
short int s_val = 32000;          // Guarantees at least 16 bits
long int l_val = 2147483647L;     // Guarantees at least 32 bits
long long int ll_val = 9000000LL; // Guarantees at least 64 bits

Format Specifiers

When interfacing with standard I/O functions like printf and scanf, specific format specifiers are required to correctly parse the binary data of an int.
  • Decimal: %d and %i are functionally identical in printf. However, in scanf, %d strictly expects base-10 input, whereas %i will auto-detect the base if the input is prefixed with 0x (hexadecimal) or 0 (octal).
  • Hexadecimal and Octal: The %x (hexadecimal) and %o (octal) format specifiers strictly expect an unsigned int. Passing a negative int to these specifiers invokes Undefined Behavior. To safely inspect the binary, hexadecimal, or octal representation of a signed int, it must be explicitly cast to an unsigned int.
#include <stdio.h>

void print_integers(void) {
    int val = 255;
    int negative_val = -255;

    // Standard decimal output
    printf("Decimal: %d\n", val); 
    printf("Decimal: %i\n", val);

    // Explicit cast to unsigned int is required to avoid Undefined Behavior
    printf("Hexadecimal: %x\n", (unsigned int)negative_val); 
    printf("Octal: %o\n", (unsigned int)negative_val);       
}
Master C with Deep Grasping Methodology!Learn More