ICT 6103
Lecture# 04
Structured Programming Language (C)
Mr. Sohel Rana
Assistant Professor
Institute of Information & Communication
Structured Programming Language (C)
- Basics of C Programming
- Operators, Data Types
- Control Structures & Loops
- Functions (Types & Recursion)
Introduction to C
- Developed by Dennis Ritchie (1972, Bell Labs)
- General-purpose, structured, procedural language
- Portable and efficient, foundation of C++, Java, Python
- Commonly used in system software (OS, compilers)
Features of C
- Simple & Structured – easy to understand
- Efficient – close to hardware, fast execution
- Portable – runs across platforms
- Rich library – pre-defined functions
- Modular – functions support reusability
Basic Structure of a C Program
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Explanation:
- #include: adds library
- main(): starting point
- printf(): displays text
- return 0;: program ends successfully
Compilation Process
1. Preprocessing → expand macros, includes
2. Compilation → converts to assembly
3. Assembly → generates object code
4. Linking → combines with libraries
5. Execution → runs machine code
C Character Set
- Alphabets (A–Z, a–z)
- Digits (0–9)
- Special symbols { } [ ] ( ) , ; " '
- Whitespace (space, tab, newline)
Identifiers & Keywords
- Identifiers: user-defined names (sum, marks)
- Keywords: 32 reserved words (int, if, return)
❌ Cannot use keywords as identifiers.
Data Types
- Basic: int, char, float, double
- Derived: arrays, pointers, structures, unions
- Void: represents “no value”
Data Types
Variables & Constants
- Variable: memory location (value can change)
int age = 25;
- Constant: fixed value
const float PI = 3.1416;
Operators in C
- Arithmetic: +, -, *, /, %
- Relational: >, <, ==, !=
- Logical: &&, ||, !
- Assignment: =, +=, -=
- Bitwise: &, |, ^, <<, >>
- Increment/Decrement: ++, --
Increment/Decrement Operators
- Post-increment: a++ (use, then increase)
- Pre-increment: ++a (increase, then use)
Example.
int a=5;
printf("%d", a++); // prints 5, then a=6
printf("%d", ++a); // increases to 7, then prints 7
Operator Precedence
int x = 5 + 3 * 2;
✔ Multiplication has higher precedence → 5 + 6 = 11
✔ Use parentheses to override
int y = (5 + 3) * 2; // 16
Input and Output
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d", num);
}
✔ scanf() requires & before variable.
✔ %d for inter, %f for fload
Control Structures
Control structures determine the flow of execution in a
program, allowing for decision-making, repetition of
code, and jumping to different parts of the program
based on conditions.
They are categorized into three main types:
- Decision-making: if, if-else, nested if, switch
- Looping: for, while, do-while
- Jump: break, continue, goto
if-else Example
int a=10, b=20;
if(a > b)
printf("a is greater");
else
printf("b is greater");
✔ Output: b is greater
switch Example
int day = 2;
switch(day) {
case 1: printf("Sunday"); break;
case 2: printf("Monday"); break;
default: printf("Other day");
}
✔ Output: Monday
Loops in C
Loops in C programming are control flow statements
used to repeatedly execute a block of code until a
specified condition is met. They are essential for
performing repetitive tasks efficiently.
• for loop: fixed iterations
• while loop: condition checked first
• do-while loop: executes at least once
for Loop Example
for(int i=1; i<=5; i++) {
printf("%d ", i);
}
✔ Output: 1 2 3 4 5
while Loop Example
int i=1;
while(i<=5) {
printf("%d ", i);
i++;
}
✔ Entry control
✔ Output: 1 2 3 4 5
do-while Loop Example
int i=1;
do {
printf("%d ", i);
i++;
} while(i<=5);
✔ Executes once before checking → Output: 1 2 3 4 5
✔ Exit Control
break and continue
for(int i=1;i<=5;i++) {
if(i==3) continue; // skips printing 3
if(i==5) break; // exits loop at 5
printf("%d ", i);
}
✔ Step: prints 1,2 → skips 3 → prints 4 → break at 5
✔ Output: 1 2 4
Functions in C
Definition: block of reusable code
Types:
- Library functions: printf(), sqrt()
- User-defined functions
Function Syntax
returnType functionName(parameters) {
// code
return value;
}
Example:
int add(int x, int y) {
return x+y;
}
✔ Called as: sum = add(3,4);
Example of Recursive Function
int fact(int n) {
if(n==0) return 1;
else return n * fact(n-1);
}
✔ fact(5) → 5×4×3×2×1 = 120
✔ Recursion calls itself until base case.
Practice Question Set
Q1: Find output
int a=5;
printf("%d", a++ + ++a);
✔ Output: 12
Q2:Find output
int i=0;
while(i<3) { printf("%d ", i); i++; }
✔ Output: 0 1 2
Q3:Find output
for(int i=1;i<=5;i++)
if(i%2==0) printf("%d ", i);
✔ Output: 2 4