Unit 1: Algorithm development and C fundamentals
Programming in C notes · PTU syllabus (BSIT102/BSBC102)
On this page
Unit summary
Programming starts with a clear algorithm and the basic building blocks of the C language. This unit covers the steps in program development, flowcharts and algorithm development, debugging, compilation and execution, input/output and assignment statements, constants, variables, operators and expressions, keywords, data types and identifiers.
After this unit you can
- Explain the steps in program development, algorithms and flowcharts
- Explain debugging, compilation and execution of C programs
- Use constants, variables, data types, keywords and identifiers
- Write expressions with operators and I/O statements
PTU syllabus topics
- Steps in program development
- flowcharts
- algorithm development
- debugging
- compilation and execution
- I/O statements
- assignment statements
- constants
- variables
- operators and expressions
- keywords
- data types and identifiers
Topic 1
Steps in program development
Good programs are planned before they are typed. Skipping the analysis and design steps usually means more time spent debugging later.
- 1
Define the problem
- 2
Analyse inputs, outputs and processing
- 3
Design the algorithm and flowchart
- 4
Code the program
- 5
Compile and remove syntax errors
- 6
Test and debug
- 7
Document
- 8
Maintain
Example
Developing a simple-interest program: define the problem (find SI); analyse — inputs P, R, T and output SI; design the algorithm (SI = P × R × T ÷ 100); code it in C; test with P = 1000, R = 5, T = 2, expecting 100; then document and maintain it.
Exam tip
Write the steps in order and draw the cycle: define → analyse → design → code → test → document → maintain.
Topic 2
Algorithms and flowcharts
An algorithm is a finite, ordered set of clear steps that solves a problem. A good algorithm has input, output, definiteness, finiteness and effectiveness. A flowchart is a diagram of an algorithm using standard symbols.
| Symbol | Name | Used for |
|---|---|---|
| Oval | Terminal | Start and stop |
| Parallelogram | Input/Output | Read or print |
| Rectangle | Process | Calculation or assignment |
| Diamond | Decision | Yes/No condition |
| Arrow | Flow line | Direction of flow |
| Small circle | Connector | Joining parts of a chart |
Pseudocode is the algorithm written in plain, structured English that looks like code but follows no strict syntax.
Example
Algorithm to find the larger of two numbers: 1. Start. 2. Read A, B. 3. If A > B, print A; else print B. 4. Stop.
Topic 3
Debugging, compilation and execution
Errors are of three kinds: syntax errors (caught by the compiler, e.g. a missing semicolon), run-time errors (occur while running, e.g. division by zero) and logical errors (the program runs but gives wrong results).
- 1
Write source code (prog.c)
- 2
Preprocessor
Expands #include and #define
- 3
Compiler
Converts to object code; reports syntax errors
- 4
Linker
Joins object code with library functions to make an executable
- 5
Loader
Loads the program into memory
- 6
Execution
Runs and produces output
- Errors: syntax errors (compiler), linker errors (missing functions), run-time errors (division by zero), logical errors (wrong output).
- Debugging: reading error messages, printing intermediate values, dry runs with trace tables, using a debugger (gdb, IDE breakpoints).
- Commands: gcc prog.c -o prog then ./prog.
Example
Three errors in one program: printf("%d", a) without a semicolon gives a syntax error; x = a / b; with b = 0 crashes at run time; writing avg = a + b / 2 instead of (a + b) / 2 runs but gives a wrong average — a logical error.
Exam tip
The compiler finds only syntax errors; testing with known inputs finds logical errors.
Topic 4
Keywords and identifiers
Keywords are reserved words with fixed meanings (int, float, if, else, while, return — 32 in standard C89). Identifiers are names given by the programmer to variables, functions and arrays.
- Character set: letters (A–Z, a–z), digits (0–9), special symbols (+ − × / % ; { } etc.) and white space.
- Identifiers are names given to variables, functions and arrays. Rules: start with a letter or underscore, contain only letters, digits and underscores, are case-sensitive, and cannot be a keyword.
total_marksis valid;2sumandfloatare not. - Keywords are 32 reserved words with fixed meaning in C, such as
int,float,if,else,while,for,return,void,struct,switch.
Example
Valid and invalid identifiers: total_marks, _count and sum2 are valid. 2sum (starts with a digit), total marks (contains a space), float (a keyword) and rate% (special symbol) are invalid.
Exam tip
Identifier rules: letters, digits and underscore only; must not start with a digit; cannot be a keyword; C is case-sensitive (Sum and sum differ).
Topic 5
Data types, constants and variables
A variable is a named memory location whose value can change; a constant is a fixed value that cannot change during execution (e.g. 3.14, 'A', "SBS", or one declared with const).
| Data type | Typical size | Format specifier | Example |
|---|---|---|---|
| int | 2 or 4 bytes | %d | 25 |
| float | 4 bytes | %f | 3.14 |
| double | 8 bytes | %lf | 3.14159265 |
| char | 1 byte | %c | 'A' |
A variable is a named memory location whose value can change: int age = 18;. A constant is a fixed value: integer (25), real (3.5), character ('x') and string ("SBS"). A symbolic constant gives a constant a name: #define PI 3.14159.
Exam tip
Sizes of int depend on the compiler (2 bytes on old 16-bit compilers, 4 bytes on modern ones). Mention this in answers.
Example
Choosing data types: int age = 19; stores a whole number; float percentage = 82.5; stores a decimal; char grade = 'A'; stores one character; const float PI = 3.14159; declares a constant that cannot be changed later.
Exam tip
Learn each type's size and format specifier — %d, %f, %c, %lf — they appear in almost every program.
Topic 6
Operators and expressions
C evaluates expressions using precedence (which operator first) and associativity (left-to-right or right-to-left for operators of equal precedence). An expression combines operands and operators to produce a value.
Arithmetic
+ − × / %
a % b gives the remainder
Unary
++ −− − sizeof
i++ adds 1
Relational
< > <= >= == !=
a >= b gives 1 or 0
Logical
&& (AND), ! (NOT), OR
(a > 0) && (b > 0)
Assignment
= += −= ×= /=
x += 5 means x = x + 5
Conditional
? :
max = (a > b) ? a : b
- Precedence decides which operator is applied first (for example × and / before + and −); associativity decides the order among operators of equal precedence (mostly left to right).
- Library functions are ready-made functions in header files:
sqrt(),pow()in math.h;printf(),scanf()in stdio.h;strlen()in string.h.
c#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Sum = %d\n", a + b); /* 13 */
printf("Remainder = %d\n", a % b); /* 1 */
printf("Bigger = %d\n", (a > b) ? a : b);
return 0;
}Example
Precedence in action: int x = 10 + 4 * 3 / 2; — multiplication and division first, left to right: 4 × 3 = 12, 12 ÷ 2 = 6, then 10 + 6 = 16. Also, 7 / 2 gives 3 (integer division) and 7 % 2 gives 1 (remainder).
Exam tip
Remember: integer division drops the decimal part; use 7.0 / 2 to get 3.5.
Topic 7
Input/output and assignment statements
Formatted I/O uses printf and scanf with format specifiers; unformatted I/O uses functions such as getchar, putchar, gets and puts that handle characters or strings without format codes.
- Assignment: variable = expression; compound forms +=, −=, *=, /=, %=.
- Formatted I/O: printf and scanf with format specifiers %d, %f, %c, %s, %lf; unformatted I/O: getchar, putchar, gets (unsafe — use fgets), puts.
c#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int sum = a + b; /* assignment */
printf("Sum = %d\n", sum);
return 0;
}Example
Reading and printing: int n; printf("Enter a number: "); scanf("%d", &n); printf("Square = %d", n * n); — for input 6, the output is Square = 36. Note the & before n in scanf.
Exam tip
The most common error: forgetting & in scanf for non-array variables.
Key terms
- Algorithm
- Finite step-by-step procedure to solve a problem
- Flowchart
- Diagram of an algorithm using standard symbols
- Compiler
- Translates source code into object code
- Identifier
- Name given to a variable or function
- Logical error
- Error giving wrong output without compiler messages
Quick revision
- Program development steps; algorithm properties; flowchart symbols.
- Preprocess → compile → link → load → execute; error types; debugging.
- Character set, 32 keywords, identifier rules.
- Data types, constants, variables; operators, precedence and expressions.
- printf, scanf, getchar, putchar; assignment.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.State the steps in program development.
- Q2.Name four flowchart symbols.
- Q3.Distinguish syntax and logical errors.
- Q4.What does the linker do?
- Q5.State the rules for naming identifiers.
- Q6.What is the difference between = and ==?
Long-answer questions
- Q1.Explain the steps in program development with algorithm and flowchart.
- Q2.Explain compilation, linking and execution of a C program and debugging.
- Q3.Explain data types, constants, variables, keywords and identifiers in C.
- Q4.Explain operators and expressions in C with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Programming in C, or to ask about studying B.Sc IT at Synetic.
