This is the start of the programming section, and we are going to start from the very beginning. If you have never written a line of code in your life, you are in the right place. If you have written Python or JavaScript before, some of this will feel familiar and some of it will feel oddly strict — we will point out the differences as we go.
The goal of this lesson is not to make you a C expert. It is to get you reading and writing small C programs comfortably, because almost everything else on this platform — exploiting a binary, reversing a program, solving a programming challenge — goes easier once C stops looking like noise.
C is old. It was written in the early 1970s, and most of the software your computer runs today is still built on top of it. The operating system, the standard library every program links against, the tools you will attack in the binary exploitation section — overwhelmingly C.
What makes C worth learning for security work is that it is close to the machine. Higher-level languages like Python do a lot of work on your behalf: they manage memory for you, check that you are not reading past the end of an array, and hide the raw bytes behind friendly objects. C does none of that. When you write C, you are deciding where bytes live, how big they are, and when they get cleaned up.
That makes C unforgiving — a small mistake can crash the program or, more interestingly, be turned into an exploit. But it also makes C honest. Nothing is hidden from you, which is exactly why it is the language for understanding how programs really work.
Python runs your code directly: you hand the interpreter a .py file and it executes it line by line. C does not work that way. You write human-readable source code in a .c file, then a compiler translates that source into a binary — a file full of machine instructions your processor can run directly. You run the binary, not the source.
Here is the traditional first program:
1#include <stdio.h>
2
3int main(void)
4{
5 printf(To turn that source into a program you can run, we hand it to gcc, a common C compiler. The -o flag names the output file:
gcc hello.c -o hello./helloHello, IronForge!Two steps: compile, then run. If you change hello.c, you have to compile again before the change takes effect — the binary does not update itself.
A few things in that short program are worth naming now; we will come back to each one:
#include <stdio.h> pulls in the standard input/output library so we can use printf. More on this in The Preprocessor.int main(void) is the entry point. Every C program starts running at main. The int means main hands back a whole number when it finishes.printf prints text. The \n at the end is a newline — C does not add one for you.return 0; ends the program and reports success. By convention, 0 means "everything went fine" and any other number means something went wrong.That is the smallest useful program. We are only using gcc as a one-liner here; the Compilation lesson covers compiler flags and build systems like Make once your projects grow past a single file.
In Python, a variable just holds "a number" and you never think about how big it is. C makes you choose. Every variable has a type, and the type decides two things: what kind of value it holds, and how many bytes it takes up in memory.
You declare a variable by writing its type, then its name:
1int count = 10;
2char letter = 'A';
3double price = 4.99;These are the types you will reach for most often:
| Type | Holds | Typical size |
|---|---|---|
char | a single byte, often a character | 1 byte |
int | a whole number | 4 bytes |
long | a larger whole number | 8 bytes |
float | a decimal number | 4 bytes |
double | a more precise decimal number | 8 bytes |
Notice the sizes say "typical." The C standard does not pin down exactly how big an int is — it only promises a minimum. That is fine for everyday code, but in security work we often care about the exact number of bytes, because we are reading raw data or reasoning about what fits in memory. For that, C gives us fixed-width types in stdint.h:
1#include <stdint.h>
2
3uint8_t byte; // exactly 1 byte, unsigned
4uint16_t halfword; // exactly 2 bytes
5The u means unsigned — the value can only be zero or positive, which doubles the largest number it can hold. Without the u a type is signed and can go negative. You will see these fixed-width types constantly once we start reading files byte by byte, so get comfortable with them early.
If you ever want to know how big a type is, sizeof tells you:
printf("an int is %zu bytes\n", sizeof(int));A program that runs top to bottom and stops is not very useful. Control flow is how we make the program decide and repeat.
To decide, we use if and else. The code in the braces runs only when the condition in the parentheses is true:
1if (score >= 60)
2{
3 printf("You passed.\n");
4}
To repeat a fixed number of times, we use a for loop. It has three parts separated by semicolons: set up a counter, keep going while a condition holds, and update the counter each time around:
1for (int i = 0; i < 5; i++)
2{
3 printf("i is %d\n",That loop prints i is 0 through i is 4. The i++ adds one to i after each pass. When the condition i < 5 is finally false, the loop stops.
When you want to repeat but do not know how many times up front — reading a file until it runs out, say — a while loop is the better fit. It keeps going as long as its condition is true:
1while (more_data_to_read)
2{
3 // handle one piece, then update the condition
4}if, for, and while cover the vast majority of what you will write. There are others, like switch for choosing between many fixed cases, but these three are the workhorses.
A function is a named, reusable piece of code. You have already met one: main. You write your own the same way — a return type, a name, a list of parameters in parentheses, and a body in braces:
1int add(int a, int b)
2{
3 return a + b;
4}This function takes two int values, named a and b, and returns their sum. You call it by name:
int total = add(3, 4); // total is now 7If a function does not hand anything back, its return type is void:
1void greet(void)
2{
3 printf("Welcome.\n");
4}There is one detail about functions that trips up newcomers, and it matters a great deal for the next lesson: C passes arguments by value. When you call add(3, 4), the function receives its own private copies of 3 and 4. If add changed a, the original would be untouched back where you called it. A function cannot reach out and modify the caller's variables just by taking them as parameters.
That limitation is exactly the problem pointers solve, which is where we are headed next.
int add(int a, int b);. This tells the compiler what to expect. Header files, which we will meet in a moment, are mostly collections of these prototypes.Before the compiler proper ever looks at your code, a step called the preprocessor runs and handles every line that starts with #. It does simple text substitution — no real logic — but two of its directives are everywhere.
#include pastes the contents of another file into yours. That is how your program gains access to library functions: #include <stdio.h> brings in the prototypes for printf and friends so the compiler knows they exist. The angle brackets mean "look in the system's standard locations"; quotes like #include "mygame.h" mean "look next to my own files."
#define creates a named constant or a small text replacement. The preprocessor swaps every use of the name for its value before compiling:
1#define MAX_PLAYERS 4
2
3int scores[MAX_PLAYERS]; // becomes int scores[4];Using a name like MAX_PLAYERS instead of a bare 4 scattered through your code makes the program easier to read and lets you change the value in one place.
That is enough syntax to start reading and writing real C. The one concept standing between you and everything interesting — memory, strings, data structures, and every challenge in this section — is the pointer. That is the next lesson.