Skip to content
My Compiler
LoginRegister

Learn C: a Beginner Tutorial

Follow these lessons in order. Each one has a short explanation and code you can run instantly in My Compiler.

  1. Lesson 1: Hello, World!

    Every language starts here: print a message to the screen to check that everything works.

    #include <stdio.h>
    
    int main(void) {
        printf("Hello, World!\n");
        return 0;
    }
    Open in compiler
  2. Lesson 2: Variables

    Variables store values such as text, whole numbers and decimals so you can reuse them.

    #include <stdio.h>
    
    int main(void) {
        char name[] = "Ada";
        int age = 21;
        double height = 1.75;
        printf("%s %d %.2f\n", name, age, height);
        return 0;
    }
    Open in compiler
  3. Lesson 3: Conditions

    if / else lets your program choose a path depending on a value.

    #include <stdio.h>
    
    int main(void) {
        int age = 18;
        if (age >= 18) {
            printf("Adult\n");
        } else if (age >= 13) {
            printf("Teenager\n");
        } else {
            printf("Child\n");
        }
        return 0;
    }
    Open in compiler
  4. Lesson 4: Loops

    Loops repeat a block of code: use for when you know the count and while when you wait for a condition.

    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 5; i++) {
            printf("%d\n", i);
        }
        int n = 3;
        while (n > 0) {
            printf("Countdown %d\n", n--);
        }
        return 0;
    }
    Open in compiler
  5. Lesson 5: Functions

    A function packs code into a reusable block that takes inputs and returns a result.

    #include <stdio.h>
    
    int add(int a, int b) {
        return a + b;
    }
    
    int main(void) {
        printf("%d\n", add(2, 3));
        return 0;
    }
    Open in compiler
  6. Lesson 6: Fibonacci sequence

    Each number is the sum of the two before it. A classic exercise that combines variables, loops and arithmetic.

    #include <stdio.h>
    
    int main(void) {
        int a = 0, b = 1;
        for (int i = 0; i < 10; i++) {
            printf("%d ", a);
            int next = a + b;
            a = b;
            b = next;
        }
        return 0;
    }
    Open in compiler