C is a powerful, low-level yet portable systems programming language created by Dennis Ritchie in 1972 at Bell Labs. It combines high-level structure with near-assembly efficiency, making it ideal for operating systems (e.g., Unix), embedded devices, and performance-critical software. This crash course covers the essentials—structure, data types, operators, functions, and compilation—without rewriting the classic "K&R" book.
A minimal C program:
#include <stdio.h> // Include standard I/O library
int main() { // Entry point; returns int
printf("Hello, world!\n"); // Print and newline
return 0; // Success exit code
}
C has built-in types; sizes vary by architecture (typically 32/64-bit):
| Type | Size (typical) | Range | Use |
|---|---|---|---|
| char | 1 byte | -128 to 127 or 0-255 | Characters, small numbers |
| int | 4 bytes | -2^31 to 2^31-1 | Integers |
| short | 2 bytes | -32,768 to 32,767 | Smaller integers |
| long | 4-8 bytes | Platform-dependent | Larger integers |
| float | 4 bytes | ~6 decimal digits | Single-precision floating point |
| double | 8 bytes | ~15 decimal digits | Double-precision |
Modifiers: signed/unsigned (for negative range), const (immutable), volatile (hardware registers).
if (x > 0) {
// do something
} else {
// alternative
}
for (int i = 0; i < 10; i++) {
// loop 10 times
}
while (condition) {
// repeat while true
}
Functions modularize code:
int add(int a, int b) { // Declaration + definition
return a + b;
}
int main() {
int sum = add(5, 3);
printf("%d\n", sum);
return 0;
}
Prototype before main if defined later: int add(int, int);
Pointers store memory addresses—key to C's power:
int x = 10;
int *p = &x; // p points to x
printf("%d\n", *p); // Dereference: prints 10
Arrays: int arr[5] = {1,2,3,4,5}; arr[0] == *(arr)
Use GCC (GNU Compiler Collection) or Clang:
gcc hello.c -o hello // Compile to executable "hello" ./hello // Run on Unix/Linux/macOS hello.exe // On Windows
Flags: -Wall (warnings), -O2 (optimize), -g (debug info).
This crash course gives you enough to write simple programs and understand vintage/modern C code. Practice with small examples—compile, run, experiment!