AnyCompiler

Variables and Types

C's primitive types include int, double, and char — every variable has a declared type, and that type is fixed once declared. printf needs a format specifier matching each type: %d for int, %.2f for a double with 2 decimal places, %c for a single char.

#include <stdio.h>

int main() {
    int age = 30;
    double price = 19.99;
    char grade = 'A';
    printf("%d %.2f %c\n", age, price, grade);
    return 0;
}
Output
30 19.99 A

How big is each type, really?

Unlike languages with a garbage collector that hides memory layout from you, C makes type sizes directly inspectable with sizeof — and they matter, since they determine exactly how much memory a variable occupies.

#include <stdio.h>

int main() {
    printf("sizeof(int) = %zu bytes\n", sizeof(int));
    printf("sizeof(double) = %zu bytes\n", sizeof(double));
    printf("sizeof(char) = %zu bytes\n", sizeof(char));
    return 0;
}
Output
sizeof(int) = 4 bytes
sizeof(double) = 8 bytes
sizeof(char) = 1 bytes

Next: operators and control flow — if/else, for, while, and switch.

Practice exercise

Declare a variable temperature of type double set to 72.5 and a variable isSunny of type int set to 1 (C has no built-in bool before stdbool.h). Print them together separated by a space.