Hello C
C is over 50 years old and still everywhere: the Linux and Windows kernels, Python's interpreter, PostgreSQL, Redis, SQLite, Git, nginx, and the firmware in cars, routers and washing machines are all written in it. C gives you direct control of memory and hardware with almost no hidden cost. That's why it's fast, and why it's the best way to learn how computers actually run programs.
💡 About the examples in this track: C can't run inside a web page, so every example shows its Output underneath. Every program was compiled with GCC 16 (
gcc -std=c23 -Wall -Wextra) on 64-bit Linux and run to produce that output exactly. Copy any example and try it yourself.
How C runs
C is compiled straight to machine code for your processor. There's no virtual machine or interpreter in between:
hello.c ──gcc──▶ hello (a native executable) ──▶ runs directly on the CPU
(you write) (machine code for this OS + CPU)The result is a small, fast program, but it only runs on the kind of system it was compiled for. To run it on Windows or an ARM board, you compile it again there.
Your first program
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}gcc -std=c23 -Wall -Wextra -o hello hello.c
./helloHello, World!
| Part | Meaning |
|---|---|
#include <stdio.h> |
Bring in the standard input/output library, which declares printf |
int main(void) |
The program starts in main. It returns an int and takes no arguments here |
{ } |
The start and end of a block |
printf("…\n"); |
Print text; \n is a newline (C doesn't add one for you) |
; |
Ends every statement |
return 0; |
The program's exit code: 0 means success |
The compiler command:
| Part | Meaning |
|---|---|
gcc |
The GNU C compiler (clang works the same way) |
-std=c23 |
Use the C23 standard, the newest (-std=c17 for older compilers) |
-Wall -Wextra |
Turn on warnings. Always use them: they catch real bugs |
-o hello |
Name the output file hello (without it, you get a.out) |
Printing values with printf
#include <stdio.h>
int main(void) {
int age = 31;
double height = 1.68;
char grade = 'A';
const char *name = "Asha";
printf("%s is %d years old\n", name, age);
printf("Height: %.2f m, grade: %c\n", height, grade);
printf("[%5d] [%-5d] [%05d]\n", 42, 42, 42);
printf("Hex: %x, octal: %o, scientific: %e\n", 255, 8, 123456.0);
printf("100%% done\n");
return 0;
}Asha is 31 years old Height: 1.68 m, grade: A [ 42] [42 ] [00042] Hex: ff, octal: 10, scientific: 1.234560e+05 100% done
| Specifier | Prints |
|---|---|
%d |
An int |
%f, %.2f |
A double (with 2 decimals) |
%c |
A single character |
%s |
A string |
%x / %o |
Hexadecimal / octal |
%5d, %-5d, %05d |
Width 5: right-aligned, left-aligned, zero-padded |
%% |
A literal % |
The format string and the values must match. printf("%d", 3.5) compiles, but prints garbage, because printf trusts you completely. Thankfully, -Wall catches it:
#include <stdio.h>
int main(void) {
printf("%d\n", 3.5);
return 0;
}main.c: In function ‘main’:
main.c:4:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
4 | printf("%d\n", 3.5);
| ~^ ~~~
| | |
| int double
| %f
-322032552The program still compiled and ran, and printed nonsense. Warnings are bugs waiting to happen: treat every one as an error (-Werror makes the compiler do exactly that).
Comments
#include <stdio.h>
int main(void) {
// A line comment
/* A block comment,
across several lines */
printf("Comments are ignored\n"); /* even here */
return 0;
}Comments are ignored
Compile errors
Leave out a semicolon and the compiler stops, pointing at the line:
#include <stdio.h>
int main(void) {
printf("Hello\n")
return 0;
}main.c: In function ‘main’:
main.c:4:22: error: expected ‘;’ before ‘return’
4 | printf("Hello\n")
| ^
| ;
5 | return 0;
| ~~~~~~Read errors from the top: the first one is usually the real problem, and later ones are often confused follow-ups. The message says where it noticed the problem (main.c:4:22: file, line, column), which is often just after the actual mistake.
Exit codes
main's return value goes back to whoever started the program, such as your shell or a script:
#include <stdio.h>
int main(void) {
printf("Something went wrong\n");
return 3;
}gcc -std=c23 -Wall -o check check.c
./check
echo "exit code: $?"Something went wrong exit code: 3
Return 0 for success and a non-zero value for failure, so scripts can react with ./check && deploy or if ! ./check; then …. You can also use EXIT_SUCCESS and EXIT_FAILURE from <stdlib.h>.
Installing a C compiler
| System | How |
|---|---|
| Ubuntu / Debian | sudo apt install build-essential (gcc, make and the standard headers) |
| Fedora / RHEL | sudo dnf install gcc make |
| macOS | xcode-select --install. This installs clang, which also answers to gcc |
| Windows | WSL (then the Ubuntu line above), or MSYS2, or Visual Studio's MSVC compiler |
Check it works with gcc --version. For an editor, VS Code with the C/C++ extension, or CLion, gives you autocompletion, error underlining and a debugger.
Why learn C?
- You'll understand everything else better. Python lists, Java objects and JavaScript strings are all built from the memory, pointers and structs you'll learn here.
- Performance and control. No garbage collector pauses, predictable memory use, direct hardware access: what operating systems, databases, games and embedded devices need.
- It's everywhere. Every other language can call C libraries, and every platform has a C compiler.
The price: C trusts you. It doesn't check array bounds or stop you from using freed memory, and mistakes become crashes or security holes. This track teaches the safe habits and the tools (warnings, sanitizers, Valgrind) that catch those mistakes.
Practice
- Write a program that prints your name, age and city with one
printfand three specifiers. - Print a small table of three products with names left-aligned in 10 characters and prices with 2 decimals.
- Make
mainreturn1, run the program and check the exit code withecho $?. - Remove the
#include <stdio.h>line from hello.c and read the error the compiler gives.
1.What does gcc -o hello hello.c produce?
2.Where does every C program start running?
3.Why compile with -Wall -Wextra?
4.What does printf("%.1f\n", 2.75); print?