Module 1 · Getting Started with C — topic 2 of 4
Programming Fundamentals
Every program — in any language — is built from the same few ideas: input, processing, output, sequence, selection and repetition. This lesson maps them to C before you write bigger programs.
Learning objectives
- Describe a problem as input, processing and output
- Recognise sequence, selection and repetition in an algorithm
- Translate a short algorithm into a C program
Definition
An algorithm is a finite, ordered set of unambiguous steps that solves a problem. A program is an algorithm written in a programming language so that a computer can execute it.
Introduction
Before typing code, a programmer decides what the program must take in, what it must compute and what it must show. Writing this down as an algorithm — numbered steps in plain language — catches logical mistakes early, when they are cheap to fix.
Computer scientists proved that any computable algorithm can be built from just three control structures: doing steps one after another (sequence), choosing between steps (selection) and repeating steps (iteration). C provides all three.
Why it matters
Most beginner bugs are not syntax errors; they are logic errors — the program does exactly what you wrote, but what you wrote is wrong. Thinking in terms of input, processing and output, and planning the control flow, is the habit that prevents them.
University exams and interviews often ask for an algorithm or flowchart before the code. Being able to move between the two is a core skill.
Detailed explanation
Input is data the program receives: from the keyboard, a file, a sensor or another program. Processing transforms that data: arithmetic, comparisons, searching, sorting. Output is the result: text on screen, a file, a signal.
Sequence means statements run in the order they are written. Selection (if, switch) runs some statements only when a condition is true. Iteration (for, while, do-while) runs statements repeatedly.
Data lives in variables: named memory locations with a type. Operations combine values into expressions, such as price * quantity. A statement such as total = price * quantity; evaluates an expression and stores the result.
Syntax
- Comments between
/*and*/are ignored by the compiler. - Each line shows one fundamental idea; real programs combine them.
&npasses the address ofnsoscanfcan store into it (explained in the input/output lesson).
Code examples
Sequence: steps in order
Area = 60
Selection: choosing a path
Pass
More examples
All three structures together
Average: 64.00 Passed: 3 of 4
Step-by-step explanation
- The array
marksholds four values;count,passedandtotalstart at known values. - The
forloop (iteration) visits each mark once using the indexifrom 0 to 3. total += marks[i];adds the current mark to the running total (sequence inside the loop).- The
if(selection) counts only marks of 40 or more. (double)total / countconverts to floating point so the division keeps decimals;%.2fprints two decimal places.
Expected output
Running “All three structures together” prints exactly:
Practical applications
- A billing system reads items (input), sums prices and applies discounts (processing), and prints an invoice (output).
- A thermostat repeatedly reads temperature (iteration) and turns heating on or off (selection).
- Exam result software applies pass/fail rules to every student record.
Best practices
- Write the algorithm as comments, then fill in the code under each comment.
- Test with simple inputs whose answer you can work out by hand.
- Give every variable a starting value before you use it.
Common mistakes
Coding before understanding the problem
Avoid
Prefer
Writing the algorithm first exposes questions such as “what if n is 0?” before they become bugs.
Integer division when a decimal is expected
Avoid
Prefer
Dividing two ints discards the fractional part. Convert one operand to double first.
Important notes
- C has no built-in input validation:
scanfhappily leaves a variable unchanged if the user types letters. Later lessons show how to check its return value.
Key takeaways
- Programs follow input → processing → output.
- Sequence, selection and iteration are enough to express any algorithm.
- Plan the algorithm in plain language before writing C.
Try it yourself
- 1Write an algorithm (numbered steps) to find the largest of three numbers, then implement it in C with fixed values.
Show hint
Use selection twice.
- 2Change the intermediate example so it also prints how many students scored above 75.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
3 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich three control structures are sufficient to express any algorithm?
- BeginnerA program reads marks, computes an average and prints it. Which part is the “processing” step?
- BeginnerWhat is printed?