All Courses
C Basics

Variables, Data Types & Formatting

Variables are named storage locations in memory. In C, every variable must have a declared data type, which determines the size and layout of the variable's memory space.

1. Variables as Memory Lockers

Think of RAM as a giant grid of storage lockers, each having a unique numeric address. A variable name is a label you slap on a locker. Instead of remembering the address 0x7ffd01, you write age.

C is a statically typed language, meaning the data type of a variable is fixed at compile time and cannot change:

int age = 25;      // Allocates 4 bytes of memory, labels it 'age', and stores 25
double price = 9.9; // Allocates 8 bytes of memory, labels it 'price', and stores 9.9

2. Basic Data Types and Sizes

The standard data types in C vary in size depending on the system architecture (e.g., 32-bit vs. 64-bit systems). The standard ranges are:

Data Type Description Typical Size Range / Value
char Single ASCII character or small integer 1 Byte (8 bits) -128 to 127 or 'A'
int Standard integer value 4 Bytes (32 bits) -2,147,483,648 to 2,147,483,647
float Single-precision floating point number 4 Bytes (32 bits) $pprox$ 6 decimal digits precision
double Double-precision floating point number 8 Bytes (64 bits) $pprox$ 15 decimal digits precision

Modifiers

You can prepend modifiers to integer types to change their size and range: - unsigned int: Removes negative values, doubling the positive range (0 to 4,294,967,295). - short int (or short): Reduces size to 2 bytes. - long int (or long): Typically 4 or 8 bytes. - long long int (or long long): Guaranteed to be at least 8 bytes.

3. Format Specifiers & String Formatting

Format specifiers tell functions like printf() (print formatted) and scanf() (read formatted) how to interpret the binary values in memory:

#include <stdio.h>

int main() {
    int age = 21;
    double height = 1.78;
    char grade = 'A';
    char name[] = "Alice";

    printf("Name: %s\n", name);
    printf("Age: %d years old\n", age);
    printf("Height: %.2lf meters\n", height); // .2 limits output to 2 decimal places
    printf("Grade: %c\n", grade);

    return 0;
}

Common specifiers: - %d / %i: int (signed) - %u: unsigned int - %f: float - %lf: double (long float) - %c: char - %s: string (character array) - %p: memory address pointer (printed in hexadecimal)

4. Reading Input with scanf

The scanf() function reads formatted input from standard input (keyboard):

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    // The & symbol (address-of) passes the memory address of age
    // so scanf knows where to write the input value in memory.
    scanf("%d", &age); 
    printf("You are %d years old.\n", age);
    return 0;
}

[!WARNING] Forgetting the & (address-of) operator in scanf() for variables like int, float, or double is a critical mistake. It will cause the program to write data to a random memory location, leading to a program crash (Segmentation Fault).