C ProgrammingUnit 125 min read
Review & Practical Sessions: C Programming Mastery
Unit 12 of C Programming consolidates all 11 units into a comprehensive review of syntax, logic, and problem-solving, with practical coding exercises (arrays, pointers, file handling) and debugging techniques for TU exams. Covers common pitfalls, optimization tips, and full program examples (e.g., student record system
Key Concepts Recap
1. Syntax and Structure Review
C programs follow a strict structure:
#include <stdio.h> // Preprocessor directive
int main() { // Main function (entry point)
// Declarations & statements
return 0; // Exit status
}
Visual: Program flow as a finite state machine (start → declarations → execution → exit).
stateDiagram-v2
[*] --> Start: main()
Start --> Declarations: variables, types
Declarations --> Execution: statements
Execution --> Exit: return 0
Exit --> [*]2. Data Types and Memory
| Type | Size (bytes) | Example | Key Use Case |
|---|---|---|---|
int |
4 | int age = 25; |
Counting, indices |
float/double |
4/8 | float pi = 3.14; |
Scientific calculations |
char |
1 | char grade = 'A'; |
Single characters, ASCII codes |
pointer |
4/8 (address) | int *ptr = &x; |
Dynamic memory, arrays |
Real-world tie: Khalti’s transaction system uses int for order IDs and float for amounts (e.g., float balance = 5000.00;).
Practical Exercises
Exercise 1: Student Record System
Task: Store 10 student names (strings) and marks (floats) in arrays, then compute average. Solution:
#include <stdio.h>
#define N 10
int main() {
char names[N][50];
float marks[N], sum = 0.0;
for (int i = 0; i < N; i++) {
printf("Enter name %d: ", i+1);
scanf("%s", names[i]);
printf("Enter marks: ");
scanf("%f", &marks[i]);
sum += marks[i];
}
printf("Average: %.2f\n", sum/N);
return 0;
}
Trace Table:
| Step | i |
names[i] |
marks[i] |
sum |
|---|---|---|---|---|
| 1 | 0 | "Ram" | 85.5 | 85.5 |
| 2 | 1 | "Sita" | 90.0 | 175.5 |
| ... | ... | ... | ... | ... |
| 10 | 9 | "Hari" | 78.0 | 823.0 |
Visual: Array state after input:
graph LR
A["names[0]"] -->|"Ram"| B["names[1]"] -->|"Sita"| C["..."]
D["marks[0]"] -->|"85.5"| E["marks[1]"] -->|"90.0"| F["..."]Exercise 2: Bank Transaction Simulator
Task: Simulate a bank account with deposit/withdraw using structures and file handling. Code:
#include <stdio.h>
typedef struct {
char name[50];
float balance;
} Account;
void saveToFile(Account acc) {
FILE *fp = fopen("account.dat", "ab");
fwrite(&acc, sizeof(Account), 1, fp);
fclose(fp);
}
int main() {
Account acc = {"John", 1000.0};
saveToFile(acc);
printf("Saved to file.\n");
return 0;
}
File State After Execution:
graph TD
A["account.dat"] -->|"Binary Data"| B["John\n1000.00"]Real-world tie: NMB Bank’s ATM uses similar file handling to store transaction histories.
Common Pitfalls and Fixes
| Mistake | Cause | Fix |
|---|---|---|
| Infinite loop | Missing i++ in for |
Always increment loop counters |
| Buffer overflow | Unchecked scanf("%s", name) |
Use fgets(name, 50, stdin) |
| Pointer segmentation fault | Dereferencing NULL pointer |
Initialize pointers: int *ptr = NULL; |
In the Real World
eSewa’s Payment Queue
- Uses FIFO queues (via linked lists) to process transactions in order.
- Example: When you pay a bill, your request joins the queue until processed.
Pathao’s Ride Allocation
- Hash tables map driver IDs to their current location for fast lookup.
- Example: When you request a ride, the app checks available drivers in O(1) time.
NTC’s Network Routing
- Graph algorithms (Dijkstra’s) find the shortest path for data packets.
- Example: Your internet traffic takes the fastest route to Google’s servers.
Exam Tip
- For TU/PU exams:
- Code tracing: Always show the state after each step (e.g., array/pointer changes).
- File handling: Remember modes (
"r","w","a") andfopenchecks. - Pointers: Draw memory diagrams for questions on
*ptrvs&var. - Structures: Use them for real-world data (e.g.,
struct Student { char name[50]; int marks; };).
- NEB tips:
- Explain time complexity (e.g., "This loop runs in O(n) time").
- For debugging questions, comment each step in your answer.
Final Challenge:
Write a program to find the longest word in a text file (use fgets + string functions). Hint: Compare lengths using strlen().
Based on the TU BIM syllabus for C Programming (IT232), unit 12.
Discussion
Loading…