Featured resource
2026 Tech Forecast
2026 Tech Forecast

1,500+ tech insiders, business leaders, and Pluralsight Authors share their predictions on what’s shifting fastest and how to stay ahead.

Download the forecast
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

Control Flow and Reusable Logic in C

A regional freight depot routes incoming orders through a small C console. Each order arrives as a short command code and a priority letter; the console must classify it, compute its shipping fee, simulate how it stacks onto pallets without ever looping forever on a bad configuration, and reorder the shipment queue when two orders need to swap priority slots. The starter project has the data structures and a skeleton main loop in place; you will fill in the decision logic, the loops, the extracted helper functions, and the pointer-based swap that the console depends on.

Lab platform
Lab Info
Level
Beginner
Last updated
Nov 17, 2026
Duration
45m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. Challenge

    Step 1: Tour the freight depot's dispatch console

    The freight depot routes every incoming order through a small C console. Each order arrives as two pieces of text: a short command code such as "PICKUP" and a single priority letter such as 'A'. Before the console can do anything else, it has to decide what those two values mean, simulate how the order's crates stack onto pallets, cost the shipment, and sometimes reorder two entries in the shipment queue. Every one of those steps is a control-flow or function-design decision you will make by hand over the next four steps. The starter project already has the console's data flow sketched out: src/classifier.c and src/classifier.h hold the command and priority classifiers, src/pallet.c holds the pallet-stacking simulation, src/fees.c holds the fee and crate-count math, src/swap.c holds the priority swap, src/queue.c holds the shipment queue reorder, and src/report.c ties them all together. Every header already declares the functions you'll implement; you'll only ever edit the matching .c file. Each stub function body currently holds a // TODO comment naming the task that finishes it. Step 2 fills in the classifiers: an if/else chain for the multi-character command codes, and a switch statement for the single-character priority letters, because those are the C constructs each shape actually calls for. Step 3 simulates pallet stacking with nested loops, then adds the safety counters that stop those loops from running forever when a pallet is misconfigured. Step 4 pulls repeated fee and crate arithmetic out of main into typed functions, then writes a swap function that looks reasonable but silently fails because its parameters are passed by value. Step 5 rewrites that swap with pointer parameters so it actually works, uses it to reorder the shipment queue, and assembles the final per-order dispatch report. Each task changes one function's behaviour, and the change is checked by running the depot console's logic against fixed inputs and inspecting what it returns. Read the TODO and Hint comments left in the stub before you start editing -- they name the exact function and the technique the task expects, not the finished code.

    info> If you get stuck, you can refer to the provided solution code for each task, available in the solution folder.

  2. Challenge

    Step 2: Classify incoming orders with if/else and switch

    Every order that reaches the depot starts as a short command code and a single priority letter. The dispatch console has to decide, deterministically, what those two pieces of text mean before anything else can happen. C gives you two different tools for that decision: an if/else chain for comparing strings with strcmp, and a switch statement for branching on a single character or small integer. Getting the choice right matters -- switch cannot compare strings directly, and an if/else chain of single-character comparisons is harder to read than the switch it should have been. src/classifier.c is where both classifiers live, and right now they are just TODO markers. With commands recognized as numbers, the console needs the same treatment for priority letters -- but a priority letter is a single character, which is exactly the shape switch was built for. Two classifiers, two ways of returning a number: now the console needs one function that calls both and turns them into the single action code everything else in the dispatch loop will branch on.

  3. Challenge

    Step 3: Simulate pallet stacking with bounded nested loops

    Orders don't just get classified -- they get physically stacked onto pallets before they leave the depot. Simulating that stacking means nested loops: an outer loop over pallets, an inner loop over the crates going onto each one. Nested loops are also where C programs most often go wrong, because nothing stops a loop from running forever except the condition you write yourself. src/pallet.c starts with a straightforward stacking simulation before it gets a safety net. count_stacked_crates works fine as long as palletCapacity is a sensible positive number. A misconfigured order -- capacity zero, or negative -- would make the inner loop's crate count never reach zero, and the outer loop would never stop. Before that happens in production, stack_with_limit adds an iteration counter that guarantees the loop always terminates. The depot's own dispatch cycle has the identical shape: keep processing batches of orders until none remain, but never past a fixed number of cycles, in case ordersRemaining never actually reaches zero.

  4. Challenge

    Step 4: Extract shared calculations into functions and expose the swap bug

    main has been recomputing the same shipping-fee formula and the same crate-count arithmetic in more than one place. Pulling that logic into named functions with typed parameters and a return value is what makes it trustworthy: one definition, one place to fix, and a value the caller can check. src/fees.c is where both extracted calculations will live. count_required_crates has the same shape as calculate_shipping_fee: read parameters in, compute locally, return a typed result. Together they replace every scattered fee-and-crate calculation main used to do inline. Not every function that changes two variables actually changes the caller's variables. When you pass an int by value, the function receives its own copy -- it can swap those copies all it wants, and the caller's originals are untouched when the function returns. src/swap.c's attempt_swap_priorities will let you see that for yourself before you fix it in the next step.

  5. Challenge

    Step 5: Fix the swap with pointers and wire up the dispatch report

    The fix for attempt_swap_priorities is to stop passing copies and start passing addresses. A pointer parameter lets a function reach back into the caller's own memory and change the value that lives there, which is exactly what reordering the shipment queue requires. src/swap.c's swap_priorities takes int* parameters instead of int. With a swap that actually works, src/queue.c's reorder_queue can use it to exchange two entries in the shipment queue array in place, using the & operator to hand swap_priorities the addresses it needs. Every helper from the last four steps -- classification, fee and crate math, pallet stacking, and the queue swap -- comes together in one place: the report the console builds for each order. src/report.c's build_dispatch_report calls each of them in turn and formats the result.

  6. 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.out
    

    The 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 test
    

    Or one task at a time:

    bash runTest.sh test_task_2_1
    

    Each 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

Pluralsight’s AI authoring technology is designed to accelerate the creation of hands-on, technical learning experiences. Serving as a first-pass content generator, it produces structured lab drafts aligned to learning objectives defined by Pluralsight’s Curriculum team. Each lab is then enhanced by our Content team, who configure the environments, refine instructions, and conduct rigorous technical and quality reviews. The result is a collaboration between artificial intelligence and human expertise, where AI supports scale and efficiency, and Pluralsight experts ensure accuracy, relevance, and instructional quality, helping learners build practical skills with confidence.

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.

Get started with Pluralsight