CBasicsbeginner

Learn C Programming Step by Step for Beginners

A first-year guide to C: structure of a program, data types, input and output, operators, loops, arrays and functions.

Published
TL;DR – Quick Answer

To learn C step by step, first understand the structure of a C program, then learn data types and variables, printf and scanf, operators, if-else, loops, arrays and functions, in that order. Write and run a small program for every topic before moving on. Pointers and structures come after these basics.

On This Page

C is one of the oldest programming languages still in wide use. Operating systems such as Linux, many embedded devices and parts of databases are written in C. Most engineering colleges in India teach it in the first year because it shows how a computer really runs a program.

This page helps you learn C programming step by step. Each step has a short program and its output. Type every program yourself and run it. Reading alone will not teach you C.

Before you start: set up a compiler

C is a compiled language. A compiler turns your .c file into a program the computer can run. You need one of these:

  • GCC on Linux or macOS, or on Windows through MinGW-w64.
  • An IDE such as Code::Blocks or Dev-C++, which comes with a compiler.
  • An online C compiler for quick practice when you are away from your laptop.

With GCC, you compile and run a file like this:

gcc hello.c -o hello
./hello

On Windows Command Prompt, run hello.exe instead of ./hello. To see why C needs this extra step and Python does not, read compiler vs interpreter.

Step 1: structure of a C program

Every C program follows the same shape. Here is the classic first program:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Output:

Hello, World!
Part What it means
#include <stdio.h> Brings in the standard input/output library, which has printf and scanf
int main(void) The starting point. Every C program runs main first
{ } Curly braces mark the start and end of a block of code
printf(...) Prints text on the screen
\n Moves to a new line
; Ends each statement
return 0; Tells the system the program ended without error

Common mistake: Forgetting the semicolon. The compiler then reports an error on the next line, which confuses beginners. Always check the line above the error too.

Step 2: variables and data types

A variable is a named box in memory that holds a value. In C, you must say what type of value each box holds before you use it.

Data type Holds Example printf format
int Whole numbers int age = 19; %d
float Decimal numbers float cgpa = 8.5f; %f
double Decimals with more precision double pi = 3.14159; %lf in scanf, %f in printf
char One character char grade = 'A'; %c
#include <stdio.h>

int main(void) {
    int age = 19;
    float cgpa = 8.5f;
    char grade = 'A';

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

Output:

Age: 19
CGPA: 8.5
Grade: A

%.1f prints one digit after the decimal point. Without .1, printf shows six digits, such as 8.500000. Note that a single character uses single quotes, 'A', while text uses double quotes, "Hello".

The size of int depends on the system. On most modern computers it is 4 bytes. You can check with sizeof(int).

Step 3: input and output with scanf and printf

printf shows output. scanf reads input typed by the user.

#include <stdio.h>

int main(void) {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum = %d\n", a + b);
    return 0;
}

Sample run:

Enter two numbers: 12 30
Sum = 42

The & before a and b gives scanf the address of each variable, so it knows where to store the numbers. Forgetting & is the most common scanf bug. The program may crash or print garbage.

Step 4: operators

Operators do work on values.

Type Operators Example Result
Arithmetic + - * / % 17 / 5 and 17 % 5 3 and 2
Relational == != > < >= <= 10 > 3 1 (true)
Logical && || ! (5 > 2) && (3 > 8) 0 (false)
Assignment = += -= *= x += 5 adds 5 to x
Increment ++ -- x++ adds 1 to x

Two points catch beginners:

  • Integer division drops the decimal. 17 / 5 is 3, not 3.4. Write 17.0 / 5 to get 3.4.
  • = and == are different. = stores a value. == compares two values.

C has no true or false by default in older code. It treats 0 as false and any other number as true. Since C99 you can include <stdbool.h> to use bool, true and false.

Step 5: decisions with if-else

#include <stdio.h>

int main(void) {
    int marks = 72;

    if (marks >= 75) {
        printf("Distinction\n");
    } else if (marks >= 60) {
        printf("First class\n");
    } else if (marks >= 35) {
        printf("Pass\n");
    } else {
        printf("Fail\n");
    }
    return 0;
}

Output:

First class

C checks each condition from top to bottom and runs the first block that is true. C also has switch for choosing between many fixed values, such as menu options 1, 2 and 3.

Step 6: loops

A loop repeats code. C has three loops: for, while and do-while.

#include <stdio.h>

int main(void) {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("%d x 7 = %d\n", i, i * 7);
    }
    return 0;
}

Output:

1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35

Use for when you know how many times to repeat. Use while when you repeat until something happens. The full comparison, with output tables, is in loops in programming.

Step 7: arrays

An array stores many values of the same type under one name. Each value has an index that starts at 0.

#include <stdio.h>

int main(void) {
    int marks[5] = {78, 85, 62, 90, 71};
    int i, total = 0;

    for (i = 0; i < 5; i++) {
        total += marks[i];
    }
    printf("Total = %d\n", total);
    printf("Average = %.2f\n", total / 5.0);
    return 0;
}

Output:

Total = 386
Average = 77.20

C does not check if an index is valid. Reading marks[5] in this program is a bug, because the last valid index is 4. The program may print a random value or crash. Learn more in what is an array.

Step 8: functions

A function is a named block of code that does one job. You write it once and call it many times.

#include <stdio.h>

int square(int n) {
    return n * n;
}

int main(void) {
    int i;
    for (i = 1; i <= 4; i++) {
        printf("Square of %d is %d\n", i, square(i));
    }
    return 0;
}

Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16

int square(int n) means the function takes one int called n and returns an int. If you write a function below main, add a declaration such as int square(int n); above main so the compiler knows about it.

Your C learning order after the basics

Order Topic Why it matters
1 Strings (char arrays) Needed for names, words and text programs
2 Pointers Core of C; used for arrays, strings and functions
3 Structures Group related data, such as a student record
4 Dynamic memory (malloc, free) Create memory while the program runs
5 File handling Read and write files
6 Basic data structures Linked list, stack and queue in C

How to practise C every day

  • Solve two or three small programs a day: even or odd, largest of three, factorial, reverse a number, prime check.
  • Predict the output before you run the program. Then compare.
  • Read every compiler warning. Compile with gcc -Wall file.c to see more of them.
  • Rewrite a program without looking once you understand it.

Start with logic problems such as checking whether a number is prime and finding the factorial with a loop. The ideas are the same in any language.

When you are comfortable with C, the next step is often C++ or Java. Read C vs C++ to see what changes, then try the C++ tutorial for beginners.

Wondering what to learn after C? Compare C vs Java to see how the two languages differ for placements and projects.

Frequently Asked Questions

Is C good for beginners?
Yes. C has a small set of keywords and teaches you how memory and data really work. It is stricter than Python, so you will see more errors at first, but those errors teach you a lot.
How long does it take to learn C basics?
Many first-year students cover the topics on this page in four to eight weeks with daily practice. Pointers and file handling take longer. What matters is that you can write programs without copying.
Which compiler should I use for C?
GCC is free and widely used. On Windows you can get it through MinGW-w64 or use an IDE such as Code::Blocks that includes a compiler. Online compilers work for quick practice.
Why does scanf need an ampersand?
scanf needs the address of the variable so it can store the value there. The & operator gives that address. Arrays of characters used as strings are an exception because the array name already acts as an address.
Should I learn C before Java or Python?
It is not required. C helps you understand memory and how programs run, which makes later languages easier. If your college starts with C, learn it well instead of skipping it.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — View the Java Full Stack curriculum

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us