What is C? Why Learn It?
C is a general-purpose, compiled, low-level language created by Dennis Ritchie at Bell Labs in 1972. It was designed to write operating systems — the Unix kernel was rewritten in C. Today, C still runs the world:
- 🐧 Linux kernel — millions of lines of C
- 🎮 Game engines, embedded systems, microcontrollers
- 🐍 Python, Ruby, PHP interpreters are built in C
- ⚙️ Databases (SQLite, PostgreSQL) are written in C
Why start with C? Because C teaches you how computers actually work — memory, pointers, bits. Every other language makes more sense after C. It forces you to think clearly.
How C Works: Compile → Run
C is a compiled language. You write source code → the compiler translates it to machine code → your CPU runs it directly. This makes C extremely fast.
- Step 1 — Write code in a .c file
- Step 2 — Compile with
gcc hello.c -o hello - Step 3 — Run with
./hello
# Compile your source file gcc hello.c -o hello # Run the compiled program ./hello # Output: Hello, World!
Your First C Program
#include <stdio.h> // Include standard I/O library int main() { // main() is where execution starts printf("Hello, World!\n"); // Print to screen return 0; // 0 = success }
#include <stdio.h>— imports the Standard Input/Output library. You need this forprintfandscanf.int main()— every C program has exactly onemainfunction. This is the entry point.printf()— prints formatted text.\nis a newline character (moves cursor to next line).return 0;— tells the OS the program finished successfully. Non-zero means error.{ }— curly braces define a block of code. Everything inside belongs to that block.
; — forgetting it is the #1 beginner mistake. The compiler will throw an error. Think of it like a period at the end of a sentence.
Variables & Data Types
In C, you must declare the type of every variable before using it. The type tells the computer how much memory to allocate and how to interpret the bits stored at that location.
| Type | Size | Range / Use | Example |
|---|---|---|---|
| int | 4 bytes | Whole numbers (±2 billion) | int age = 25; |
| float | 4 bytes | Decimal numbers (~7 digits) | float pi = 3.14f; |
| double | 8 bytes | Precise decimals (~15 digits) | double x = 3.14159; |
| char | 1 byte | Single character / small int | char c = 'A'; |
#include <stdio.h> int main() { int age = 20; float gpa = 3.85f; char grade = 'A'; double salary = 52000.50; printf("Age: %d\n", age); // %d = integer printf("GPA: %.2f\n", gpa); // %.2f = 2 decimal places printf("Grade: %c\n", grade); // %c = character printf("Salary: %.2f\n", salary); // %lf for double in scanf return 0; }
printf how to display a value:%d → int %f → float/double %c → char %s → string %ld → long
Getting Input from the User
scanf() reads input from the keyboard. Notice the & before variable names — this gives scanf the address of the variable in memory, so it knows exactly where to store the value you type. (This is your first glimpse at pointers — for now, just always remember the &.)
#include <stdio.h> int main() { int age; char name[50]; // Array of 50 chars = a string printf("Enter your name: "); scanf("%s", name); // No & needed for arrays printf("Enter your age: "); scanf("%d", &age); // & gives the address of age printf("Hello, %s! You are %d years old.\n", name, age); return 0; }
Arithmetic & Expressions
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 5 + 3 | 8 |
| - | Subtract | 10 - 4 | 6 |
| * | Multiply | 6 * 7 | 42 |
| / | Divide | 9 / 2 | 4 (integer!) |
| % | Modulus (remainder) | 9 % 2 | 1 |
#include <stdio.h> int main() { int a = 9, b = 2; printf("%d + %d = %d\n", a, b, a + b); printf("%d / %d = %d\n", a, b, a / b); // = 4, not 4.5! printf("%d %% %d = %d\n", a, b, a % b); // = 1 // For real division, cast to float: printf("%.2f\n", (float)a / b); // = 4.50 return 0; }
9 / 2 gives 4, not 4.5. The decimal is simply dropped, not rounded. Cast to (float) or (double) when you need the full decimal result.
Quick Quiz — Test Yourself
What does return 0; in main() mean?
What is the output of: printf("%d", 7 / 2); ?
Which format specifier is used to print a float?
Why do we write &age inside scanf()?
Lesson Checklist
- I understand what C is and why it matters
- I know the compile → run workflow with gcc
- I can read and write a Hello World program
- I understand #include, main(), printf, and return 0
- I know the 4 basic data types: int, float, double, char
- I can use format specifiers (%d, %f, %c, %s)
- I can read user input with scanf()
- I understand integer division truncation
- I completed the quiz