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:
| Parameter | Type | Description |
|---|---|---|
| %d / %i | Format Specifier | Signed 32-bit decimal integer (4 bytes on modern architectures). |
| %f / %.Nf | Format Specifier | Floating point number; .N controls exact fractional precision decimal places. |
| %lf | Format Specifier | Double-precision 64-bit floating point number (8 bytes). |
| %c | Format Specifier | Single byte character encoded as ASCII integer. |
| %lu | Format Specifier | Unsigned long integer for byte sizes and 64-bit counts. |
Ready-to-Run Code Example
Executable in CompilraRun 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 decodingRelated C Tutorials & Compiler
C Operators: Arithmetic, Relational, and Bitwise Logic
Master C operators including arithmetic, bitwise shifts, modulus, and logical boolean checks.
C Standard I/O: printf and scanf Formatting Guide
Format output with printf and read console user input safely with scanf and fgets.
C Type Casting: Explicit Type Conversion with Examples
Convert data types in C explicitly using (target_type) and understand integer promotions.
C Online Compiler
Open in Compilra IDE
Run and test C on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.