The Basic Data Types

C needs to know what kind of value each variable holds. Here are the types you will use most, with the printf placeholder for each:

TypeWhat it holdsExamplePlaceholder
intWhole numbersint year = 2025;%d
doubleNumbers with a decimal pointdouble price = 9.99;%f
floatDecimals, less exact than doublefloat temp = 36.6;%f
charOne single character, in single quoteschar grade = 'A';%c
#include <stdio.h>

int main() {
    int year = 2025;
    double price = 9.99;
    char grade = 'A';

    printf("Year: %d\n", year);
    printf("Price: %f\n", price);
    printf("Grade: %c\n", grade);
    return 0;
}

Output

Year: 2025
Price: 9.990000
Grade: A

Choosing how many decimals

%f always shows 6 decimals. That's often too many. Write %.2f to show 2 decimals, %.1f for 1, and so on. The number is rounded for you:

#include <stdio.h>

int main() {
    double price = 9.5;
    double pi = 3.14159;
    printf("%f\n", price);
    printf("%.2f\n", price);
    printf("%.3f\n", pi);
    printf("%.0f\n", pi);
    return 0;
}

Output

9.500000
9.50
3.142
3

Whole numbers vs decimals

An int cannot hold a decimal part. If you put one in, the part after the point is simply cut off (not rounded!):

#include <stdio.h>

int main() {
    int whole = 7.9;       // the .9 is thrown away
    double exact = 7.9;
    printf("%d\n", whole);
    printf("%.1f\n", exact);
    return 0;
}

Output

7
7.9

Characters are really small numbers

A char holds one character, like 'A' or '7' or '?'. Inside the computer, each character is stored as a number (its ASCII code). Print it with %c to see the letter, or with %d to see the number:

#include <stdio.h>

int main() {
    char letter = 'A';
    printf("%c\n", letter);
    printf("%d\n", letter);
    return 0;
}

Output

A
65

Common mistake: Single and double quotes are different in C. 'A' (single quotes) is one character. "A" (double quotes) is text, called a string. We'll meet strings in Module 8.

True and false

Old C has no special type for yes/no. It simply uses numbers: 0 means false, and any other number means true. Newer C adds bool if you include <stdbool.h>, but it still prints as 1 or 0:

#include <stdio.h>
#include <stdbool.h>

int main() {
    bool isOpen = true;
    bool isRaining = false;
    printf("Open: %d\n", isOpen);
    printf("Raining: %d\n", isRaining);
    return 0;
}

Output

Open: 1
Raining: 0

Remember: int + %d for whole numbers, double + %f for decimals, char + %c for a single character.

HOME LABS COMMUNITY DASHBOARD