C Programming Tutorial 0/65 lessons ~6 min read Lesson 9

    Variables

    Variables are named regions of memory with a fixed type.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Variables are named regions of memory with a fixed type. C requires a declaration before first use so the compiler knows how much space to reserve.

    Understanding the topic

    Declaration Specify type and name: int count;

    Initialization Assign at declaration: int count = 10; or later: count = 10;

    Naming rules Letters, digits, underscore; cannot start with a digit; case-sensitive.

    • Declaration — Specify type and name: int count;.
    • Initialization — Assign at declaration: int count = 10; or later: count = 10;.
    • Naming rules — Letters, digits, underscore; cannot start with a digit; case-sensitive.

    Step-by-step explanation

    1. Declaration — Specify type and name: int count;.
    2. Initialization — Assign at declaration: int count = 10; or later: count = 10;.
    3. Naming rules — Letters, digits, underscore; cannot start with a digit; case-sensitive.

    Syntax reference

    Syntax reference:

    c
    type variableName;
    type variableName = value;

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(void) {
    int years = 30;
    float rate = 2.5f;
    char band = 'B';
    printf("%d %.1f %c\n", years, rate, band);
    return 0;
    }

    Output

    30 2.5 B

    Execution workflow

    1Variables — step by step
    1 / 3

    Declaration

    Specify type and name: int count;.

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Declare variables of different types
    • Print values with correct format specifiers

    Summary

    Variables in C — Typed, named storage — declare before use.

    Ready to mark this lesson complete?Track your journey across the entire course.