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
- 1Write source code
.c file
- 2Preprocessor
Expands #include and #define
- 3Compiler
Converts to object code
- 4Linker
Adds library code, makes executable
- 5Loader
Loads into memory and runs
Topic 1
Steps in program development
- 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
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
- 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.
Topic 4
Keywords and identifiers
- 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.
Topic 5
Data types, constants and variables
| 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.
Topic 6
Operators and expressions
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;
}Topic 7
Input/output and assignment statements
- 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;
}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.
