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 long keyword in C is a fundamental integer data type (implicitly long int) that guarantees a minimum storage size of 32 bits. It provides a larger or equal value range compared to the standard int type, depending on the compiler and the target architecture’s data model.

Memory Size and Data Models

The exact byte size of long is implementation-defined. While the C standard mandates a minimum of 32 bits, its actual size diverges across different 64-bit environments:
  • ILP32 (32-bit systems): 32 bits (4 bytes).
  • LLP64 (64-bit Windows): 32 bits (4 bytes).
  • LP64 (64-bit Unix/Linux/macOS): 64 bits (8 bytes).

Syntax and Modifiers

By default, long is signed, utilizing two’s complement representation. It can be explicitly modified using the unsigned keyword to shift the representable range to strictly non-negative values, effectively doubling the maximum positive limit.
// Signed long declarations (all are semantically identical)
long a;
long int b;
signed long c;
signed long int d;

// Unsigned long declarations (all are semantically identical)
unsigned long e;
unsigned long int f;

Literal Suffixes

When parsing integer literals, the C compiler assigns the int type by default if the value fits. To explicitly force a literal to be evaluated as a long, append the L or l suffix (uppercase L is standard practice to avoid visual confusion with the digit 1). For unsigned variants, combine U and L.
long signed_val = 2147483647L;
unsigned long unsigned_val = 4294967295UL;

Format Specifiers

When interfacing with standard I/O functions like printf and scanf, specific length modifiers (l) must be prepended to the integer conversion specifiers to prevent undefined behavior resulting from stack misalignment or truncation.
  • %ld or %li: Signed long
  • %lu: Unsigned long
  • %lx or %lX: Unsigned long in hexadecimal
  • %lo: Unsigned long in octal
#include <stdio.h>

int main(void) {
    long val = -123456L;
    unsigned long uval = 123456UL;

    printf("Signed: %ld\n", val);
    printf("Unsigned: %lu\n", uval);
    
    return 0;
}

Standard Limits

The absolute minimum and maximum representable values for the specific compilation target are exposed via macros in the <limits.h> standard library header.
#include <limits.h>

int main(void) {
    long min_val = LONG_MIN;            // Guaranteed to be <= -2147483647
    long max_val = LONG_MAX;            // Guaranteed to be >= +2147483647
    unsigned long umax_val = ULONG_MAX; // Guaranteed to be >= +4294967295
    
    return 0;
}
Master C with Deep Grasping Methodology!Learn More