Lab · C Programming (from zero to systems)
Lab: a canteen thali bill
The brief
The canteen at an engineering college in Nagpur wants a small program that prints a thali bill with GST. You install and check gcc, print the sizes of the basic types, read the order with scanf, print the bill with printf, and reject bad input.
Why it matters
Every C job starts with the same loop: write, compile with warnings on, run and read the output. Billing counters, ticket machines and meter readers all read numbers, check them and print an aligned result. Getting printf and scanf right (the right specifier, the & and the return value) is the first thing campus placement tests check.
Skills you practise
- gcc with -Wall -Wextra
- sizeof and %zu
- scanf with %d and %lf
- printf with %.2f and %%
- checking scanf's return value
- exit status
You need
- gcc (Linux, macOS with Xcode command line tools, or Windows with MSYS2 or WSL)
- A text editor, for example VS Code
What you will have at the end
canteen.c: a program that compiles with no warnings, prints the type sizes and a GST thali bill, and rejects bad input with exit status 1.
How it is marked (rubric, 100 points)
| Correct outputEvery check passes: sizes, amount 136.40, CGST and SGST 3.41 each, total 143.22, and the error line for bad input. | 45 |
|---|---|
| Clean buildgcc -std=c17 -Wall -Wextra prints no warnings at all. | 20 |
| Right specifiers%zu for sizeof, %lf in scanf, %.2f in printf and %% for a percent sign, and you can say why each is needed. | 20 |
| Safe inputscanf's return value is checked and the program exits with status 1 on bad input. | 15 |
0/5 steps done
Step 1 of 5
Check your compiler
gcc --version prints a gcc (or Apple clang) version line.
Before writing any C, check that the compiler is installed and on your PATH. On Ubuntu or WSL run sudo apt install build-essential; on Windows with MSYS2 install the mingw-w64 gcc package; on macOS run xcode-select --install (there, gcc is really clang, which is fine for this course). Then run gcc --version and paste what it prints.
$ gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0$ gcc --versionYour notes: canteen.c
Write down each command you ran. It goes into your project summary.