Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 9 of 10
Mini Project Part 1 - hello.c Dissected & Unit Converter
In this project we write two programs. First we fully dissect the classic `hello.c` - every token explained. Then we build a unit converter for temperature and weight, applying primitive types, printf/scanf, and type casting in a real context.
hello.c - Every Token Explained
/* === hello.c - fully annotated === */
/* Token 1: #include
Preprocessor directive. Pastes the CONTENTS of stdio.h
into this file before the compiler sees it.
stdio.h declares: printf, scanf, FILE, EOF, etc. */
#include <stdio.h>
/* Token 2: int
The return type of main. main() MUST return int. */
/* Token 3: main
The entry point. The OS calls main() when your
program starts. Execution begins at the FIRST STATEMENT
inside main - not at the top of the file. */
/* Token 4: (void)
Means: this function accepts NO arguments.
Using () without void is technically "unspecified args"
in C89. Use (void) to be explicit. */
int main(void) {
/* Token 5: printf
Declared in stdio.h. Writes formatted text to stdout.
"Hello, World!
" - string literal in read-only memory.
- escape sequence for newline (ASCII 10). */
printf("Hello, World!
");
/* Token 6: return 0
Returns 0 to the operating system.
Convention: 0 = success, non-zero = error.
The shell reads this: echo $? → 0 */
return 0;
} /* Token 7: closing brace - end of main's body */
/* Build: gcc -Wall -std=c99 hello.c -o hello
Run: ./hello
Check: echo $? → should print 0 */Unit Converter - Applying Types & I/O
/* unit_converter.c - temperature and weight conversion */
#include <stdio.h>
/* Conversion functions - well-defined math, no global state */
double celsius_to_fahrenheit(double c) { return c * 9.0 / 5.0 + 32.0; }
double fahrenheit_to_celsius(double f) { return (f - 32.0) * 5.0 / 9.0; }
double kg_to_pounds(double kg) { return kg * 2.20462; }
double pounds_to_kg(double lbs) { return lbs / 2.20462; }
int main(void) {
int choice;
double input, result;
printf("=== Unit Converter ===
");
printf("1. Celsius -> Fahrenheit
");
printf("2. Fahrenheit -> Celsius
");
printf("3. Kilograms -> Pounds
");
printf("4. Pounds -> Kilograms
");
printf("Choose (1-4): ");
if (scanf("%d", &choice) != 1 || choice < 1 || choice > 4) {
fprintf(stderr, "Error: invalid choice
");
return 1;
}
printf("Enter value: ");
if (scanf("%lf", &input) != 1) {
fprintf(stderr, "Error: invalid number
");
return 1;
}
switch (choice) {
case 1: result = celsius_to_fahrenheit(input);
printf("%.2f C = %.2f F
", input, result); break;
case 2: result = fahrenheit_to_celsius(input);
printf("%.2f F = %.2f C
", input, result); break;
case 3: result = kg_to_pounds(input);
printf("%.2f kg = %.4f lbs
", input, result); break;
case 4: result = pounds_to_kg(input);
printf("%.4f lbs = %.2f kg
", input, result); break;
default: fprintf(stderr, "Unreachable
"); return 1;
}
return 0;
}
/* Build: gcc -Wall -Wextra -std=c99 unit_converter.c -o converter
Run: ./converter
Try: 100 C -> 212 F, 70 kg -> 154.3 lbs */Common Mistakes
- Not checking scanf return value - if the user types 'abc' when you expect a number, `scanf` returns 0 and the variable is left uninitialised. Always check `if (scanf(...) != expected_count)`.
- Using `int` for the output when the input is floating-point - if `celsius_to_fahrenheit` returned `int`, `100 * 9 / 5 + 32 = 212` by luck, but `36.6 * 9 / 5 + 32 = 97` (truncated to 97). Use `double` for all scientific computation.
- Forgetting to check the range of user input - `choice < 1 || choice > 4` is the guard that prevents out-of-range switch values. Without it, a rogue input hits the default and the program misbehaves.
Tip
Tip
Practice Mini Project Part 1 helloc Dissected Unit Converter in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Every C program begins execution in main() with standard exit codes returned to the OS environment
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Part 1 helloc Dissected Unit Converter from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Part 1 helloc Dissected Unit Converter is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- In this project we write two programs.
- Not checking scanf return value - if the user types 'abc' when you expect a number, `scanf` returns 0 and the variable is left uninitialised. Always check `if (scanf(...) != expected_count)`.
- Using `int` for the output when the input is floating-point - if `celsius_to_fahrenheit` returned `int`, `100 * 9 / 5 + 32 = 212` by luck, but `36.6 * 9 / 5 + 32 = 97` (truncated to 97). Use `double` for all scientific computation.
- Forgetting to check the range of user input - `choice < 1 || choice > 4` is the guard that prevents out-of-range switch values. Without it, a rogue input hits the default and the program misbehaves.
Finished this lesson?