C:C Primitive Data Types and Explicit Format Specifiers
C•Variables

C Primitive Data Types and Explicit Format Specifiers

In C, variables are typed allocations in memory with strict byte widths and alignment rules. Because C has no automatic string interpolation, format specifiers in printf (%d, %f, %.2f, %c, %lu, %s) explicitly dictate how raw binary memory is decoded and rendered.

Syntax

int count = 42; float temp = 98.6f; double precise = 3.14159265; char grade = 'A'; unsigned long bytes = 1048576UL; printf("%d | %.2f | %c | %lu\n", count, temp, grade, bytes);

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
%d / %iFormat SpecifierSigned 32-bit decimal integer (4 bytes on modern architectures).
%f / %.NfFormat SpecifierFloating point number; .N controls exact fractional precision decimal places.
%lfFormat SpecifierDouble-precision 64-bit floating point number (8 bytes).
%cFormat SpecifierSingle byte character encoded as ASCII integer.
%luFormat SpecifierUnsigned long integer for byte sizes and 64-bit counts.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing C locally:

#include <stdio.h>

int main(void) {
    // 1. Primitive integer and character data types
    int activeSockets = 128;
    char systemTier = 'A';
    unsigned long memoryAllocatedBytes = 1048576UL;

    // 2. Floating-point types with defined precision
    float cpuTemperature = 48.35f;
    double benchmarkScore = 99.87654;

    // 3. Explicit printf format specifiers
    printf("=== GCC C Memory Allocation & Formats ===\n");
    printf("Active Sockets:   %d (sizeof: %lu bytes)\n", activeSockets, sizeof(activeSockets));
    printf("Cluster Tier:     %c (ASCII code: %d)\n", systemTier, systemTier);
    printf("Allocated Memory: %lu bytes (sizeof: %lu bytes)\n", memoryAllocatedBytes, sizeof(memoryAllocatedBytes));
    printf("CPU Temperature:  %.2f deg C\n", cpuTemperature);
    printf("Benchmark Score:  %.4f\n", benchmarkScore);

    return 0;
}
Expected Output:
=== GCC C Memory Allocation & Formats ===
Active Sockets:   128 (sizeof: 4 bytes)
Cluster Tier:     A (ASCII code: 65)
Allocated Memory: 1048576 bytes (sizeof: 8 bytes)
CPU Temperature:  48.35 deg C
Benchmark Score:  99.8765

How It Works: Step-by-Step

  • Lines 5-7: Allocates primitive integer, char, and unsigned long variables on the stack.
  • Lines 10-11: Initializes single-precision float (4 bytes) and double-precision float (8 bytes).
  • Lines 14-19: Maps variables to explicit format specifiers (%d, %c, %lu, %.2f, %.4f) and evaluates sizeof memory sizes.

Key Concepts

  • C does not perform runtime type introspection; printf relies entirely on the format specifier to determine how many bytes to read off the stack.
  • sizeof is a compile-time operator that returns the byte footprint as a size_t, printed via the %lu or %zu specifier.
  • Characters in C are 1-byte integers storing ASCII values; printing with %c shows the glyph, while %d reveals the decimal ASCII index.
  • The .N modifier in %.2f sets the rounding precision to exactly N decimal places instead of the default 6 digits.

Common Mistake: Mismatched format specifier reading corrupted stack memory

Passing a float to %d or an integer to %f causes printf to interpret the binary bit patterns with the wrong type decoding, resulting in zero, garbage integers, or unexpected NaN values.

❌ Incorrect
float val = 48.5f;
printf("Value: %d\n", val); // Bug: prints corrupted integer!
✅ Correct
float val = 48.5f;
printf("Value: %.2f\n", val); // Correct: matches float decoding

Related C Tutorials & Compiler

Run and test C on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

C Online Compiler