Variables & Data Types in C

    Atul Kabra3 min readUpdated

    A variable in C is a named box in memory that holds a value, and its data type decides what kind of value fits in that box and how much memory it uses. Before you can use a variable in C, you must declare it with a type — for example int age; reserves space for a whole number called age.

    What is a variable?

    Declaring a variable tells the compiler its name and type. You can declare and assign in one line:

    int score = 90;        /* declare and initialize together */
    float price;           /* declare now... */
    price = 49.5f;         /* ...assign later */
    

    A variable name must start with a letter or underscore, and can contain letters, digits, and underscores. C is case-sensitive: Score and score are different variables.

    The core data types

    C's built-in types fall into a few families. The exact sizes depend on the platform, but typical values on a 64-bit system are shown below.

    TypeHoldsTypical sizeFormat specifier
    intwhole numbers4 bytes%d
    floatsingle-precision decimals4 bytes%f
    doubledouble-precision decimals8 bytes%lf
    chara single character1 byte%c

    You can also add modifiers like short, long, and unsigned to change range, e.g. unsigned int (no negatives) or long long (a wider integer).

    A complete example

    #include <stdio.h>
    
    int main(void) {
        int   age = 19;          /* whole number */
        float height = 5.8f;     /* the f suffix marks a float literal */
        double pi = 3.14159;     /* more precise decimal */
        char  grade = 'A';       /* single character in single quotes */
    
        /* Each format specifier must MATCH its variable's type */
        printf("Age: %d\n", age);
        printf("Height: %.1f feet\n", height);  /* .1 = one decimal place */
        printf("Pi: %lf\n", pi);
        printf("Grade: %c\n", grade);
    
        return 0;
    }
    

    Notice 'A' uses single quotes (a char), while text like "Pi:" uses double quotes (a string). Mixing them up is a classic beginner error.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    Constants and the const keyword

    If a value should never change, mark it const:

    const double GRAVITY = 9.8;  /* the compiler stops you reassigning it */
    

    This documents intent and lets the compiler catch accidental edits.

    Choosing the right type

    • Counting things? Use int.
    • Money or measurements with decimals? Prefer double over float for accuracy.
    • A single letter or symbol? Use char.
    • Storing text? You need an array of char — see Strings in C.

    Common mistakes

    • Wrong format specifier: printing a double with %d (or an int with %f) gives garbage. Match the specifier to the type.
    • Forgetting to initialize: an uninitialized local variable holds a random value, not zero. Always assign before reading.
    • Single vs double quotes: 'A' is a char; "A" is a string (two bytes including the null terminator).
    • Integer division surprise: 5 / 2 is 2, not 2.5, because both operands are integers. Use 5.0 / 2 to get 2.5.
    • Float precision: 0.1 + 0.2 is not exactly 0.3. Never compare floats with ==.

    FAQ

    float or double? Use double by default; it has more precision. Use float only when memory is tight.

    Can a variable change type? No. In C a variable's type is fixed at declaration.

    Keep exploring at the hub C Programming, then Operators in C and If-Else in C.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides