LearnLearn CC in Practice

Learn C Capstone

gcc - a hands-on Linux lab on a real virtual machine.

Write a 4-function C utility from scratch to a spec, graded by compiler warnings, an input matrix, and valgrind.

Learn C Capstone: wcx

This is the track finale. You will write wcx, a small cousin of the classic wc utility, from an empty main to a working, leak-free binary. Nothing here is new. Every piece, argc and argv, fopen and the NULL check, reading a file byte by byte, stderr and exit codes, and the valgrind habit, comes from a lesson you already finished.

The contract: ./solution FILE prints one line to stdout, LINES WORDS BYTES FILE, fields separated by single spaces and ended with a single newline. The flags -l, -w, and -c print just that one count and the filename. A line is a newline byte. A word is an unbroken run of characters that are not spaces, tabs, or newlines. When the file cannot be opened, print strerror(errno) to stderr and exit 2, the same convention you learned in File I/O. When the flag is not one you recognize, print a usage line to stderr and exit 64. On both error paths, stdout stays silent.

Structure the program as four functions: count_stats reads the file once and produces all three counts, print_result formats one output line, parse_args decides the mode and the filename, and main wires them together and picks the exit code. The grader never reads your source. It grades behavior only. But a capstone written as one giant main is a capstone you cannot debug, and you will want to debug.

The classics that sink first attempts: touching argv before checking argc, counting a gap of three spaces as three word boundaries, miscounting the file that ends without a trailing newline (its words and bytes count, the missing line does not), sending the error message to stdout instead of stderr, and leaving a FILE handle open or a malloc unfreed. The hidden suite checks every one of these, and valgrind gets the last word.

TEST compiles your code with gcc -Wall and runs the two visible sample tests. Use it as often as you like. ATTEMPT runs the full hidden suite: eleven tests covering every flag, the edge fixtures, both error paths, and a clean valgrind pass. XP is awarded when ATTEMPT reaches 100 percent.

Practice Learn C Capstone in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.