Lesson 1 Progress
0%
Lesson 1  ยท  Getting Started

Hello, World! Your First C Programs

Every C programmer starts here. This lesson breaks down the traditional Hello World program line by line, then builds on it with two small programs that introduce variables and user input.

Hello World, line by line
Compilation pipeline
Multiply two integers
Multiply two decimals
๐Ÿ“–

What Is the Hello World Program?

A Hello World program is the traditional first step for learning C. It's a sanity check to confirm your environment is set up correctly, and a simple but complete introduction to syntax, header files, functions, and the compilation process โ€” all in five lines.

hello.c
hello.c
C
#include <stdio.h>

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

Step-by-Step Breakdown

1

#include <stdio.h> โ€” The Preprocessor Directive

Tells the preprocessor to include the Standard Input/Output header before compilation. It brings in the tools needed for input and output. Without it, the compiler wouldn't recognize printf() at all.

2

int main() โ€” The Entry Point

Every C program must have a main() function โ€” execution always begins here. int means the function returns a whole number to the operating system on completion; () marks it as a function. C is case-sensitive โ€” Main or MAIN will not compile.

3

{ and } โ€” Program Block Identifiers

Curly braces mark the start and end of a logical block. All code belonging to main() must sit inside these braces.

4

printf("Hello, World!\n") โ€” The Output Function

printf() ("print formatted") displays text on screen. The text in double quotes is a string literal; \n is an escape sequence that moves the cursor to a new line. The semicolon ; tells the compiler the statement is complete โ€” every statement needs one.

5

return 0 โ€” Graceful Exit

Tells the operating system the program finished successfully. 0 means "no errors"; any non-zero value signals that something went wrong.

๐Ÿ’ก Behind the scenes โ€” execution flow:
1. Preprocess#include is replaced with stdio.h's real content
โ†’
2. CompileC code becomes machine instructions
โ†’
3. Linkconnects to the C standard library so printf() works
โ†’
4. ExecuteOS calls main(), runs line by line to return 0;
program 2
2

Multiply Two Integers

Variables + scanf()

Building on Hello World, this program introduces variables and user input. It uses int for whole numbers and the asterisk * as the multiplication operator.

multiply_int.c
multiply_int.c
C
#include <stdio.h>

int main() {
    int num1 = 0;
    int num2 = 0;
    int product = 0;

    printf("Enter first integer: ");
    scanf("%d", &num1);
    printf("Enter second integer: ");
    scanf("%d", &num2);

    product = num1 * num2;   // * is the multiplication operator

    printf("The product is: %d\n", product);
    return 0;
}
program 3
3

Multiply Two Decimal Numbers

Same logic, float type

The second version swaps int for float to handle decimals โ€” everything else follows the same structure.

multiply_float.c
multiply_float.c
C
#include <stdio.h>

int main() {
    float num1 = 0.0;
    float num2 = 0.0;
    float product = 0.0;

    printf("Enter first decimal number: ");
    scanf("%f", &num1);
    printf("Enter second decimal number: ");
    scanf("%f", &num2);

    product = num1 * num2;   // Same * operator, but with decimals

    printf("The product is: %.2f\n", product);   // %.2f = 2 decimal places
    return 0;
}
terminal โ€” sample output
output
Enter first integer: 6
Enter second integer: 7
The product is: 42

Enter first decimal number: 3.5
Enter second decimal number: 2.0
The product is: 7.00
FeatureInteger VersionFloat Version
Data typeint (whole numbers)float (decimals)
Format specifier%d%f / %.2f
Initial value00.0
Operator**
Example input4 and 54.5 and 2.3
Example output2010.35
โš ๏ธ Common mistake โ€” forgetting the & in scanf. Always write scanf("%d", &num1), never scanf("%d", num1). The & (address operator) tells scanf exactly where in memory to store the value. Forgetting it can crash the program or produce garbage output.
quiz
Q

Quick Quiz

Question 1 of 4

What does #include <stdio.h> do?

Question 2 of 4

Why must the function be named exactly "main" in lowercase?

Question 3 of 4

What happens if you forget the & in scanf("%d", &num1)?

Question 4 of 4

What does %.2f mean in a printf format string?

โœ“

Lesson Checklist

  • I can explain what #include <stdio.h> does
  • I understand why every program needs int main()
  • I know what { } and ; are for
  • I can explain what return 0; means
  • I can name the 4 stages: preprocess, compile, link, execute
  • I can write a program that reads two integers and multiplies them
  • I can do the same with floats and format the output with %.2f
  • I know why & is required in scanf
  • I completed the quiz
โ†’

Next: Lesson 2 โ€” Data Types & Operators

Coming up
  • ๐Ÿ”ข int, float, char, double explained Lesson 2
  • โž• Arithmetic & assignment operators Lesson 2
  • ๐Ÿ” Type conversion basics Lesson 2