Skip to content

First Compile

You'll build a small C program with every warning switched on, fix each one, then step through it line by line in a debugger.

About

Most people's first C program compiles with a shrug and a dozen warnings they never read. Here you turn all of them on and fix every one, because in C a warning is a bug that hasn't happened yet, and then you watch the program run one line at a time in gdb or lldb. Both tools are free and already on most machines, or one package install away. Any editor is fine, and so is a Raspberry Pi or a free cloud shell if your laptop makes it awkward. You don't need to understand pointers yet, because that's the next badge.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Novice (never tried it)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own

Requirements

  1. 1
    Write a short C program that reads numbers from the keyboard and prints their average. Use fgets into a fixed buffer, a loop and a running total, and stop on an empty line or end of input. Keep it under 40 lines so you can hold the whole thing in your head. Take a screenshot of the source and a run with five numbers. Photo
  2. 2
    Compile with -Wall -Wextra -Werror and fix every warning until it builds clean. Run gcc -Wall -Wextra -Werror -g average.c -o average, or clang with the same flags. -Werror turns every warning into a hard stop, which is the point. The usual first ones are an unused variable, a comparison between signed and unsigned, and a format string that doesn't match its argument. Fix the cause, not the message, and don't cast the problem away. Take a screenshot of the warnings before and the clean build after, with the flags visible. Photo
  3. 3
    Set a breakpoint in the debugger and step through the loop, printing a variable each time round. Run gdb ./average or lldb ./average, then break main, run, and use next to walk forward one line at a time. Print total after each step. The -g flag from the build is what gives the debugger line numbers, so if you only see addresses, go back and add it. Film a short clip of stepping through three loop iterations with the value changing. Video
  4. 4
    Make it crash on purpose with a null pointer, then find the line from inside the debugger. Add int *p = NULL; *p = 5; somewhere in the program, build it with the same flags so -g is still on, and run it. Outside the debugger you get the words Segmentation fault and nothing else. Inside it you get the exact line, and bt (backtrace) shows how you got there. You'll use this about forty times in the badges after this one. Take a screenshot of the backtrace with the offending line. Photo
  5. 5
    Build it with a two-rule Makefile and explain each flag in a comment. One rule builds the program, one rule cleans up, and a CFLAGS variable holds your warning flags so you never type them again. Write a comment above CFLAGS that says in your own words what -Wall, -Wextra, -Werror and -g each do. Take a screenshot of the Makefile and of make running from a clean folder. Photo