45 Mins

2. C Variables and Data Types

Core Concept

A variable is a named container that holds a value in memory. Like C++, C is a statically typed language — you must declare the type of every variable before using it. The compiler uses this information to allocate the correct amount of memory.

This explicit typing catches errors at compile time, making C programs more reliable and faster than dynamically typed languages. Understanding C data types is the foundation of writing efficient C code.

Declaring Variables

In C, you declare a variable by specifying its type followed by the variable name. You can optionally initialize it with a value at the same time.

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

int main() {
    int age = 25;              // Integer (whole number)
    double height = 5.9;       // Double (decimal number)
    char grade = 'A';          // Character (single letter)
    bool isActive = true;      // Boolean (true/false) - requires stdbool.h
    
    printf("Age: %d
", age);
    printf("Height: %.1f
", height);
    printf("Grade: %c
", grade);
    printf("Active: %d
", isActive);
    return 0;
}
OutputAge: 25 Height: 5.9 Grade: A Active: 1

Notice that int holds whole numbers, double holds decimal numbers, char holds a single character (always in single quotes), and bool holds true or false. In C, bool outputs as 1 (true) or 0 (false).

Variable Naming Rules

Variable names in C must follow specific rules. Breaking these rules will cause a compilation error.

C
#include <stdio.h>

int main() {
    // Valid variable names
    int my_age = 30;
    int _score = 100;
    int playerHealth = 75;
    int PlayerHealth = 100;    // Different from playerHealth
    
    printf("my_age: %d
", my_age);
    printf("_score: %d
", _score);
    printf("playerHealth: %d
", playerHealth);
    printf("PlayerHealth: %d
", PlayerHealth);
    return 0;
}
Outputmy_age: 30 _score: 100 playerHealth: 75 PlayerHealth: 100

Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive. C keywords like int, struct, and return cannot be used as variable names.

Common Data Types

C provides several basic data types, each with a specific size and range of values. The exact size can vary depending on the system.

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

int main() {
    int integerNum = 42;           // Whole numbers (typically 4 bytes)
    float floatNum = 3.14f;        // Single-precision decimal (4 bytes)
    double doubleNum = 3.14159;    // Double-precision decimal (8 bytes)
    char letter = 'A';             // Single character (1 byte)
    bool flag = true;              // True or false (1 byte)
    
    printf("Integer: %d
", integerNum);
    printf("Float: %f
", floatNum);
    printf("Double: %lf
", doubleNum);
    printf("Char: %c
", letter);
    printf("Bool: %d
", flag);
    return 0;
}
OutputInteger: 42 Float: 3.140000 Double: 3.141590 Char: A Bool: 1

Use int for whole numbers, float for decimal numbers when memory is a concern, double for more precision in calculations, char for single characters, and bool for logical values.

Type Modifiers

C allows you to modify basic data types with keywords that change their size or range. These modifiers give you fine control over memory usage.

C
#include <stdio.h>

int main() {
    short int shortVar = 32767;      // 2 bytes, smaller range
    long int longVar = 2147483647;   // 4 or 8 bytes, larger range
    unsigned int unsignedVar = 4294967295U;  // Only positive values
    
    printf("Short: %d
", shortVar);
    printf("Long: %ld
", longVar);
    printf("Unsigned: %u
", unsignedVar);
    return 0;
}
OutputShort: 32767 Long: 2147483647 Unsigned: 4294967295

short reduces memory usage for small integers, long increases range for larger numbers, and unsigned stores only positive values, doubling the maximum positive range.

Constant Variables

Use the const keyword to declare variables whose values should never change. This is essential for values like PI, tax rates, or configuration settings.

C
#include <stdio.h>

int main() {
    const double TAX_RATE = 0.08;
    const int MAX_SCORE = 100;
    
    double price = 49.99;
    double total = price + (price * TAX_RATE);
    
    printf("Price: $%.2f
", price);
    printf("Total: $%.2f
", total);
    printf("Max Score: %d
", MAX_SCORE);
    return 0;
}
OutputPrice: $49.99 Total: $53.99 Max Score: 100

Attempting to change a const variable after declaration will cause a compilation error. This catches bugs early and makes your code more maintainable.

The sizeof Operator

The sizeof operator returns the size (in bytes) of a data type or variable. This is useful for understanding memory usage and for portable code.

C
#include <stdio.h>

int main() {
    printf("Size of char: %lu byte(s)
", sizeof(char));
    printf("Size of int: %lu byte(s)
", sizeof(int));
    printf("Size of float: %lu byte(s)
", sizeof(float));
    printf("Size of double: %lu byte(s)
", sizeof(double));
    printf("Size of long: %lu byte(s)
", sizeof(long));
    
    int x = 42;
    printf("Size of variable x: %lu byte(s)
", sizeof(x));
    
    return 0;
}
OutputSize of char: 1 byte(s) Size of int: 4 byte(s) Size of float: 4 byte(s) Size of double: 8 byte(s) Size of long: 8 byte(s) Size of variable x: 4 byte(s)

sizeof is a compile-time operator that evaluates to the size in bytes. The actual sizes may vary depending on the system architecture and compiler.

Knowledge Check

Ready to test your understanding of 2. C Variables and Data Types?

Back