Clarvo Blog·Programming

C Programming from Scratch: A Beginner Roadmap from Variables to Pointers

Learn how C programs are structured and progress through data types, operators, control flow, functions, arrays, strings, pointers, memory and files.

Clarvo
Clarvo Editorial Team
15 min read·21 Sept 2026·667 words
C Programming from Scratch: A Beginner Roadmap from Variables to Pointers
Source image via the referenced Medium article ↗

1. Compile your first program

hello.cc
#include <stdio.h>

int main(void) {
    printf("Hello, Cameroon!\n");
    return 0;
}
Compile with GCCbash
gcc hello.c -Wall -Wextra -o hello
./hello

The compiler translates C source into machine code. Warnings matter: keep -Wall -Wextra during learning so suspicious code is visible early.

2. Variables and formatted output

types.cc
#include <stdio.h>

int main(void) {
    int age = 19;
    double score = 16.5;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Score: %.1f\n", score);
    printf("Grade: %c\n", grade);
    return 0;
}

3. Read input carefully

input.cc
#include <stdio.h>

int main(void) {
    double score;
    printf("Enter your score: ");

    if (scanf("%lf", &score) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    printf("You entered %.1f\n", score);
    return 0;
}

4. Conditions

grade decisionc
if (score >= 16) {
    printf("Excellent\n");
} else if (score >= 12) {
    printf("Good\n");
} else if (score >= 10) {
    printf("Pass\n");
} else {
    printf("Needs more work\n");
}

5. Loops

for loopc
for (int i = 1; i <= 5; i++) {
    printf("Study session %d\n", i);
}
while loopc
int attempts = 3;
while (attempts > 0) {
    printf("Attempts left: %d\n", attempts);
    attempts--;
}

6. Functions

function.cc
#include <stdio.h>

double percentage(double score, double total) {
    if (total <= 0) return 0;
    return (score / total) * 100.0;
}

int main(void) {
    printf("%.1f%%\n", percentage(15, 20));
    return 0;
}

7. Arrays

scores.cc
#include <stdio.h>

int main(void) {
    int scores[] = {12, 15, 18, 10, 14};
    int count = sizeof(scores) / sizeof(scores[0]);
    int sum = 0;

    for (int i = 0; i < count; i++) {
        sum += scores[i];
    }

    printf("Average: %.2f\n", (double)sum / count);
    return 0;
}

8. Strings are character arrays

safe string inputc
#include <stdio.h>
#include <string.h>

int main(void) {
    char name[50];
    printf("Name: ");

    if (fgets(name, sizeof name, stdin)) {
        name[strcspn(name, "\n")] = '\0';
        printf("Hello %s\n", name);
    }
    return 0;
}

9. Pointers: addresses and dereferencing

pointer.cc
#include <stdio.h>

int main(void) {
    int score = 15;
    int *pointer = &score;

    printf("score = %d\n", score);
    printf("through pointer = %d\n", *pointer);

    *pointer = 18;
    printf("updated score = %d\n", score);
    return 0;
}
student structc
#include <stdio.h>

typedef struct {
    char name[50];
    double score;
} Student;

int main(void) {
    Student learner = { "Amina", 17.5 };
    printf("%s: %.1f\n", learner.name, learner.score);
    return 0;
}

11. Dynamic memory

malloc/freec
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int count = 5;
    int *scores = malloc((size_t)count * sizeof *scores);
    if (!scores) return 1;

    for (int i = 0; i < count; i++) scores[i] = i * 2;

    free(scores);
    scores = NULL;
    return 0;
}

12. Files

write a result filec
#include <stdio.h>

int main(void) {
    FILE *file = fopen("result.txt", "w");
    if (!file) return 1;

    fprintf(file, "Amina,17.5\n");
    fclose(file);
    return 0;
}

13. Common mistakes table

MistakeSymptomBetter habit
Using = instead of ==Condition changes a variableRead warnings and write clear conditions
Array out of boundsCrash or corrupted dataTrack length and validate indexes
Forgotten freeMemory leakDefine ownership for each allocation
Wrong scanf formatUndefined behaviourMatch the format specifier to the variable type
Uninitialised variableRandom-looking resultInitialise before use

14. Mini-project: grade analyser

grade_analyser.cc
#include <stdio.h>

#define MAX_STUDENTS 100

int main(void) {
    double scores[MAX_STUDENTS];
    int count;

    printf("Number of students (1-%d): ", MAX_STUDENTS);
    if (scanf("%d", &count) != 1 || count < 1 || count > MAX_STUDENTS) {
        printf("Invalid count.\n");
        return 1;
    }

    double total = 0;
    double highest = -1;

    for (int i = 0; i < count; i++) {
        printf("Score %d: ", i + 1);
        if (scanf("%lf", &scores[i]) != 1 || scores[i] < 0 || scores[i] > 20) {
            printf("Invalid score.\n");
            return 1;
        }
        total += scores[i];
        if (scores[i] > highest) highest = scores[i];
    }

    printf("Average: %.2f/20\n", total / count);
    printf("Highest: %.2f/20\n", highest);
    return 0;
}

Continue learning

Sources and further reading

Clarvo articles are independently written. These references let you inspect the original inspiration and primary or official sources used to deepen the guide.

  1. Source inspiration — C Programming Tutorial for Beginners — Learn C from Scratchmedium.com