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.
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Step-by-Step Breakdown
#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.
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.
{ 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.
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.
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.
Multiply Two Integers
Building on Hello World, this program introduces variables and user input. It uses int for whole numbers and the asterisk * as the multiplication operator.
#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; }
Multiply Two Decimal Numbers
The second version swaps int for float to handle decimals โ everything else follows the same structure.
#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; }
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
| Feature | Integer Version | Float Version |
|---|---|---|
| Data type | int (whole numbers) | float (decimals) |
| Format specifier | %d | %f / %.2f |
| Initial value | 0 | 0.0 |
| Operator | * | * |
| Example input | 4 and 5 | 4.5 and 2.3 |
| Example output | 20 | 10.35 |
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.Quick Quiz
What does #include <stdio.h> do?
Why must the function be named exactly "main" in lowercase?
What happens if you forget the & in scanf("%d", &num1)?
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
- ๐ข int, float, char, double explained Lesson 2
- โ Arithmetic & assignment operators Lesson 2
- ๐ Type conversion basics Lesson 2