- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Create a Minimal C Program
Station Console is a small C utility that remote weather station operators run once per shift. It takes a raw Celsius reading and a station name, validates the reading, converts it to Fahrenheit, and appends a formatted line to the shift's log. Station names come from an international roster and may contain multi-byte UTF-8 characters such as accents, so every buffer operation must be measured in bytes, not assumed characters, and every copy must respect the destination buffer's real capacity. You will build the startup status logic, the conversion functions, the input-to-output pipeline, and the safe string log assembly that main() will eventually call.
Lab Info
Table of Contents
-
Challenge
Step 1: Orient yourself in the Station Console project
Station Console
Field technicians at remote weather stations run Station Console once per shift: it takes a raw Celsius reading and a station name, validates the reading, converts it to Fahrenheit, and appends a formatted line to the shift log. Station names come from an international roster, so they may contain multi-byte UTF-8 characters such as accents — every buffer operation in this lab is measured in bytes, never assumed characters.
Over the next four steps you will write the C functions that main() eventually calls: a startup status check, a small set of temperature functions, an input-parsing-and-formatting pipeline, and a safe string-joining routine for the log itself. ## How the source is laid out
Your work lives entirely under
src/, split into four modules that build on each other:console_status.c/.h— decides and describes the exit status a station reports at startup.temperature_math.c/.h— pure functions for converting, validating, and averaging temperatures.reading_console.c/.h— parses raw numeric input and formats a converted report, calling intotemperature_math.h.station_log.c/.h— copies and joins strings into fixed buffers, calling intoreading_console.h.
Each header already declares the functions you'll complete, with an include guard, so the corresponding test file can link against them as soon as the body exists. ## What's ahead
Step 2 starts with the smallest piece: turning two basic-type readings (a sensor flag and a battery percentage) into one of a few defined exit status codes. From there, each step adds one layer — temperature math, then parsing and formatting, then the UTF-8-aware string handling that ties a station name to its reading in the final log line. Every task you complete is called, directly or indirectly, by the one after it.
info> If you get stuck, you can refer to the provided solution code for each task, available in the
solutionfolder. -
Challenge
Step 2: Report the console's startup exit status
Exit statuses from basic-type checks
console_status.hdeclares a handful ofintconstants such asSTATUS_OK,STATUS_SENSOR_FAULT, andSTATUS_LOW_BATTERY. A startup check in C is usually nothing more than comparing a few basic-type variables — anintflag, anintpercentage — against thresholds and returning the matching constant. Getting the order of those comparisons right matters: whichever condition you check first wins when more than one is true. ## From a code to a messageA status constant is only useful to the program; a technician needs a string. The second half of this module maps each constant back to readable text, with a fallback for anything unexpected.
-
Challenge
Step 3: Organize temperature logic into callable functions
Small functions, clear contracts
temperature_math.hdeclares three independent functions, each with explicit parameters and a return value — no shared state, no side effects. Splitting temperature logic this way means every piece can be tested, and reused, on its own. First up is the most fundamental one: converting a single value from Celsius to Fahrenheit. ## Validating a readingNot every number a sensor produces is safe to log.
is_reading_in_rangetakes the same kind of basic-type parameters — a value and two bounds — and returns an int-as-boolean result, the same conventionconsole_status.cused for its status codes. ## Summarizing a shiftA shift often produces several readings, not one.
average_readingstakes a pointer to an array and a count — the standard C way of passing a variable-length list to a function — and must handle the edge case of an empty batch without crashing. -
Challenge
Step 4: Read a reading and format its converted output
From text to a number
Raw input arrives as a line of text, not a
double.reading_console.hdeclaresparse_celsius_input, which uses the standard library'sstrtodto convert text to a number while also detecting text that isn't numeric at all — the difference between a valid reading and an operator's typo. ## Formatting the converted valueOnce you have a Celsius double, you need a line of output a technician can read.
format_fahrenheit_reportcalls the conversion function from Step 3 and writes the result into a caller-supplied buffer withsnprintf, which — unlikesprintf— never writes past the buffer's stated size. ## One pipeline, one resultbuild_reading_reportis the function main() will actually call: it strings parsing and formatting together and reduces the outcome to a single success/failure flag, so nothing downstream needs to know how the parsing or formatting happened underneath. -
Challenge
Step 5: Assemble the station's log entry safely
Capacity is not the same as length
A destination buffer's capacity — how many bytes it can physically hold — is a fact about the buffer. A string's length, from
strlen, is a fact about the data.copy_station_namehas to reconcile the two: it must never write more bytes than the buffer can hold, and it must always leave room for the terminating null byte, regardless of how long the incoming name is. ## Truncating without breaking a characterUTF-8 encodes some characters, like accented letters, as two or more bytes. If a truncation cuts a buffer off in the middle of one of those byte sequences, the result is invalid text. Continuation bytes in UTF-8 always have their top two bits set to
10, which is exactly the checkjoin_log_lineneeds before it decides where to stop. ## Wiring it togetherWith copying and joining in place,
build_log_entryis the last piece: it callsbuild_reading_reportfrom Step 4 to get the reading text, thenjoin_log_lineto combine it with the station name, producing exactly what a shift log expects for one line. -
Challenge
Step 6: Run the application
Every task is done, and each one was graded the moment you completed it. What you have not done yet is watch the whole application run at once -- which is the only place the pieces you wrote separately become a single working program.
Start the application
Open the Terminal tab and run:
make app && ./build/release/lab.outThe program runs in the terminal and prints its output there. Every behaviour it shows is code you wrote in the previous steps, so read the output against what each task asked for -- that is the whole lab, working as one program. ### Run the checks yourself
The lab ran each task's test for you as you went, but they are ordinary tests and you can run them whenever you like. The whole suite:
make testOr one task at a time:
bash runTest.sh test_task_2_1Each failure names the task it belongs to, so the output tells you which step to go back to rather than which line to stare at. Two things worth trying before you finish: break something on purpose and watch the matching check fail, then put it back. Knowing what a failure looks like is worth as much as knowing what a pass looks like, and it costs you nothing here.
When you are done, everything you wrote is still in the editor tabs -- the lab is yours to keep reading.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.