Skip to content

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.

Beginner 12 min read·Lesson 2 of 37 in C Programming

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.

IdeaPlain-language stepC construct
InputRead the marksscanf
SequenceThen compute the totalstatements in order
SelectionIf the average ≥ 40, passif / else
IterationRepeat for each subjectfor, while
OutputShow the resultprintf

Syntax

C
/* 1. input */ scanf("%d", &n);
/* 2. process */ result = n * n;
/* 3. selection */ if (result > 100) { /* ... */ }
/* 4. iteration */ for (int i = 0; i < n; i++) { /* ... */ }
/* 5. output */ printf("%d\n", result);
  • Comments between /* and */ are ignored by the compiler.
  • Each line shows one fundamental idea; real programs combine them.
  • &n passes the address of n so scanf can store into it (explained in the input/output lesson).

Code examples

Sequence: steps in order

C
#include <stdio.h>
int main(void) {
int length = 12;
int width = 5;
int area = length * width;
printf("Area = %d\n", area);
return 0;
}
Output
Area = 60

Selection: choosing a path

C
#include <stdio.h>
int main(void) {
int marks = 67;
if (marks >= 40)
printf("Pass\n");
else
printf("Fail\n");
return 0;
}
Output
Pass

More examples

All three structures together

C
#include <stdio.h>
int main(void) {
int marks[] = {78, 35, 91, 52};
int count = 4, passed = 0, total = 0;
for (int i = 0; i < count; i++) {
total += marks[i];
if (marks[i] >= 40)
passed++;
}
printf("Average: %.2f\n", (double)total / count);
printf("Passed: %d of %d\n", passed, count);
return 0;
}
Output
Average: 64.00
Passed: 3 of 4

Step-by-step explanation

  • The array marks holds four values; count, passed and total start at known values.
  • The for loop (iteration) visits each mark once using the index i from 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 / count converts to floating point so the division keeps decimals; %.2f prints two decimal places.

Expected output

Running “All three structures together” prints exactly:

TextOutput
Average: 64.00
Passed: 3 of 4

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

/* start typing and hope */

Prefer

/* 1. read n marks 2. sum them 3. average = sum / n 4. print */

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

printf("%d\n", total / count);

Prefer

printf("%.2f\n", (double)total / count);

Dividing two ints discards the fractional part. Convert one operand to double first.

Important notes

  • C has no built-in input validation: scanf happily 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

  1. 1
    Write an algorithm (numbered steps) to find the largest of three numbers, then implement it in C with fixed values.
    Show hint

    Use selection twice.

  2. 2
    Change 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?
Practise this topic