0% found this document useful (0 votes)
12 views14 pages

C Programming

Uploaded by

m.ahadsharief
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

C Programming

Uploaded by

m.ahadsharief
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Programming — Coursework & Plan

Course length: 15 weeks (one semester)


Structure: Weekly lecture (1.5 hr) + weekly lab/practical (2 hr)
Language: C (ISO C99/C11 features optional)
Tools: GCC/Clang, Visual Studio Code (or Code::Blocks), Git & GitHub, Makefile
(optional)

Course Learning Outcomes


By the end of this course students will be able to:

1. Write, compile and debug C programs using basic I/O, decision constructs and loops.
2. Use arrays, strings and understand memory layout and pointer arithmetic.
3. Modularize code into functions, use recursion and understand storage classes.
4. Design and use structures, unions and handle files.
5. Apply dynamic memory allocation correctly and safely.
6. Use Git/GitHub for version control and collaborate on a small project.
7. Build a small real-time style project (prepaid billing) following software engineering
practices including testing, documentation and iterative delivery.

Weekly plan (suggested 15-week schedule)


Week 1 — Intro & Setup

 Lecture: Course overview; C language history; compiling & running programs;


toolchain (gcc, make), IDE setup.
 Lab: Hello World; printf, scanf basics; exercises: read integer & print sum, simple
formatting.
 Deliverable: Short setup verification commit to GitHub repo.

Week 2 — Unformatted I/O & Basic Data types

 Lecture: printf/scanf format specifiers, pitfalls of scanf, buffer and newline


behavior.
 Lab: Programs using scanf for ints, floats, chars, strings (char arrays). Input
validation.
 Exercise: Temperature converter, simple calculator.

Week 3 — Decision Making (if / if-else / nested if / switch)

 Lecture: if, if...else, nested if, switch, fall-through behavior.


 Lab: Implement grade calculator, day-of-week program using switch.
 Exercise: Loan eligibility & BMI classification programs.

Week 4 — Looping Constructs & Unconditional Branching

 Lecture: while, do-while, for, break, continue, return. Complexity/termination.


 Lab: Loops exercises: factorial (iterative), sum of digits, prime checking.
 Self-study: formatted I/O, goto and else if ladder (read only, discouraged usage).

Week 5 — 1D Arrays & Basics of Strings

 Lecture: 1D arrays declaration, initialization, indexing, bounds issues.


 Lab: Array sum/average, find max/min, linear search, insertion into array, simple sort
(bubble).
 Exercise: Reverse array, rotate array.

Week 6 — 2D Arrays & Strings without std lib

 Lecture: 2D arrays, row-major layout, passing arrays to functions.


 Lab: Matrix addition, matrix multiplication, transpose.
 Lab exercise: Implement strlen, strcpy, strcmp manually (without std lib
functions).

Week 7 — Midterm Theory + Midterm Practical

 Midterm exam (theory) covering Modules 1–2.


 Lab test: small coding problems (arrays, loops, basic I/O).

Week 8 — Functions (modular programming)

 Lecture: Monolithic vs Modular programs, user-defined vs standard functions,


prototypes, categories (no args/no return, args/return), scope.
 Lab: Factorial (recursive + iterative), gcd (Euclid) recursive, modularize previous
programs into functions.
 Exercise: Implement simple math library functions in mylib.c/h.

Week 9 — Parameter Passing & Storage Classes

 Lecture: Call by value vs call by reference (using pointers), storage classes auto,
extern, static, register (historical).
 Lab: Swap via call-by-value and call-by-reference, counters with static, program
showing extern variables across files.
 Exercise: Small multi-file project skeleton to demonstrate extern and static.

Week 10 — Structures & Unions

 Lecture: struct definition, initialization, array of structs, nested structures, union


and its use-cases, memory layout, sizeof.
 Lab: Student record management using struct, implement functions to
add/search/display students.
 Self-study: bit-fields, differences between structures and arrays for memory.

Week 11 — Advanced: Pointers & Pointer-to-struct

 Lecture: Pointer arithmetic, pointer to pointer, pointer to structure, pointer-to-function


basics, arrays and pointers relationship.
 Lab: Pointer exercises: dynamic array allocation with malloc, realloc, free, linked
list basics (single-linked).
 Exercise: Implement function pointers for selecting sorting algorithm at runtime.

Week 12 — File Handling in C

 Lecture: FILE*, fopen, fclose, fread, fwrite, fprintf, fscanf, random access
(fseek, ftell).
 Lab: Read/write text files, CSV parsing simple, write binary file for structures and
read back.
 Self-study: storing structures to files, random access file operations.

Week 13 — Dynamic Memory & Advanced Pointers

 Lecture: Heap vs stack, memory leaks, tools to debug (valgrind mention), common
pitfalls.
 Lab: Dynamic 2D arrays, implement dynamic string functions, free memory correctly.
 Exercise: Build a small memory-managed data structure (resizable vector).

Week 14 — Git/GitHub, Versioning & DevOps intro

 Lecture: Git basics, commits, branching, merging, PR workflow, semantic commit


messages, .gitignore, README, Licenses.
 Lab: Each student creates repo, commits weekly labs, creates branches, opens PRs,
merges after code review by a peer.
 Exercise: Tag a release, push to remote, review contribution graph.

Week 15 — Final Project Presentations & Course Wrap-up

 Student project demo (prepaid telecom billing C program following SE principles),


code review and evaluation, final submission.
 Final written exam/practical (depending on course weighting).

Module-wise Coursework, Labs & Example


Problems
Below each module I list Lecture topics, Lab tasks (graded), and Self-study tasks.
Module 1 — Managing I/O, Decisions & Loops
Lectures cover: printf, scanf, if, if-else, nested if, switch, while, do-while, for,
break, continue, return.

Core lab tasks (graded):

1. Input validation program: repeatedly prompt until valid integer entered (use scanf
return value).
2. Grade classification: read marks and print grade using nested if.
3. Menu-driven calculator using switch.
4. Prime numbers between 1..N using for and while.
5. Implement for-based Fibonacci and do-while variant.

Assessment problems (short):

 Demonstrate difference between break and continue.


 Fix bugs in sample code that misuses scanf.

Self-study:

 Formatted I/O (fprintf, fscanf, %*d, width specifiers), goto (read and explain why
discouraged), else if ladder implementation.

Module 2 — Arrays & Strings


Lectures cover: 1D arrays, 2D arrays, string operations without built-ins.

Core lab tasks (graded):

1. Implement reverse_string(char s[]) without library functions.


2. Implement concat of two strings into a third buffer (check overflow).
3. Matrix multiplication and verify correctness with test matrix.
4. Implement insert and delete operations in an array.

Self-study:

 Multi-dimensional arrays, full set of string handling functions: strlen, strcpy,


strcat, strcmp, strrev.

Module 3 — Functions & Storage Classes


Lectures cover: modularization, prototypes, parameter passing, recursion, storage classes
(auto, extern, static, global variables).
Core lab tasks:

1. Break a large monolithic program into multiple files with headers: main.c, utils.c,
utils.h.
2. Recursive vs iterative implementations for factorial and Fibonacci; compare stack
usage.
3. Demonstrate static variable maintaining value across function calls.

Self-study:

 Program design concepts: top-down design, pseudo-code, flow-charting.

Module 4 — User-defined Functions, Structures & Unions


Lectures cover: elements and categories of user-defined functions, call-by-value/reference,
struct and union.

Core lab tasks:

1. Student database using struct with add/update/delete/display functions (store in


array).
2. Use a union to store either an integer id or a pointer to an extended record —
illustrate memory sharing.
3. Pass array of structs to functions and demonstrate modifications.

Self-study:

 Passing strings/arrays to functions, sizeof structures, bit-fields.

Module 5 — Pointers, Dynamic Memory & File I/O


Lectures cover: pointers, pointer arithmetic, pointer to pointer, pointer to structure, pointer
to function, dynamic memory allocation (malloc, calloc, realloc, free), file handling.

Core lab tasks:

1. Implement a dynamic array (vector) with push/pop and realloc.


2. Linked list CRUD operations (create, insert, delete, traverse).
3. Save array of struct records as binary file and read back (use fwrite/fread).
4. Implement function-pointer-based callback (e.g., a map function that accepts a
function pointer).

Self-study:

 Store structures to files, random access files using fseek, ftell.


Final Realtime Project (Term Project)
Title: Prepaid Telecom Subscriber Billing System (C) — Realistic mini-project

Goal: Build a console-based system that simulates prepaid subscriber billing and recharge,
following software engineering practices and using Git for version control and CI-like checks
(manual grading).

Requirements (minimum):

1. Maintain subscriber records (struct): phone number, balance, plan details, usage
history.
2. Simulate call events and data usage that deduct from balance with tariff rules.
3. Refill/recharge facility (multiple recharge denominations), maintain transaction
history.
4. Persist data in files (binary) between runs. Support backup and restore.
5. Provide reports: current balance, last N transactions, low-balance alert.
6. Demonstrate modularity: separate files for core functions, I/O, file operations,
utilities.
7. Unit testing: A set of test input files and expected outputs (simple).
8. Use GitHub: project repo, frequent commits, branches for features, PRs for merges.

Software engineering & XP practices to follow:

 Iterative delivery (minimum viable product first).


 Pair programming or code review (peer reviews via GitHub PRs).
 Simple test plan + test cases.
 A short README with build/run instructions and design decisions.
 Basic documentation in code and separate design doc.

Deliverables:

 Source code + Makefile (or build instructions).


 README + design doc (1–2 pages): data structures, tariff rules, file format.
 Test cases (input files and expected outputs).
 GitHub repository link (must show commit history and branches).
 Demo video (2–5 minutes) optional but recommended.

Grading rubric (example):

 Functionality: 40% (meets requirements, correctness)


 Code quality & modularity: 15%
 File handling & persistence: 10%
 Tests & documentation: 10%
 GitHub usage & commit hygiene: 10%
 Presentation & demo: 10%
 Total: 100%
Assessment & Marking Scheme (suggested)
 Midterm written & practical: 25%
 Weekly labs & assignments (averaged): 25%
 Final project (term project): 30%
 Final written exam/practical: 20%

Lab assessment: Each lab graded for correctness, code style, documentation & commit
history.

GitHub Code Versioning — Practical Guide


(for students)
Getting started (short commands):

1. git init — initialize repo (or git clone <url> to start from remote)
2. git add <files> — stage files
git add . to stage all
3. git commit -m "feature: add matrix multiplication" — commit
4. git branch feature-billing — create branch
5. git checkout feature-billing — switch branch (or git switch)
6. git push origin feature-billing — push branch
7. Create Pull Request on GitHub, request peer review, then merge.

Best practices:

 Commit small increments with meaningful messages.


 Use branches for features/fixes.
 Include .gitignore for compiled files and temporary files.
 Keep README updated and include build/run/test instructions.
 Tag releases for major milestones: git tag -a v1.0 -m "MVP completed"

Grading of Git usage:

 Proper commit history and meaningful messages: 4/10


 Use of branches and PRs: 3/10
 README + project documentation: 3/10
Example lab assignment templates &
starter skeletons
Example 1 — Swap by reference (starter)
/* swap.c */
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main(void) {
int x = 10, y = 20;
printf("Before: x=%d y=%d\n", x, y);
swap(&x, &y);
printf("After: x=%d y=%d\n", x, y);
return 0;
}

Example 2 — Dynamic vector header skeleton


/* vector.h */
#ifndef VECTOR_H
#define VECTOR_H

typedef struct {
int *data;
size_t size;
size_t capacity;
} Vector;

void vector_init(Vector *v);


void vector_push(Vector *v, int val);
int vector_get(Vector *v, size_t idx);
void vector_free(Vector *v);

#endif

(Students implement these in vector.c and write tests.)

Sample grading rubric for labs / code


quality
 Correctness / passes tests: 50%
 Code readability & comments: 15%
 Proper memory management (no leaks): 15%
 Modularization (separate files, headers): 10%
 Git commit history & README: 10%

Suggested Textbooks & Resources


(You can adapt these to your library)

 "The C Programming Language" — Kernighan & Ritchie (classic).


 "C: A Modern Approach" — K. N. King (good explanations).
 Online: GNU gcc manual, GitHub docs (for Git basics).
(Provide institutional links if needed.)

Quick checklist for instructors (what to


hand to students)
 Syllabus + weekly plan (this document)
 Lab specs (detailed per-week task descriptions)
 Grading rubrics
 Project brief + sample test cases
 GitHub classroom or repo template (optional)

If you want, I can:

 produce detailed lab handouts for each week (with input/output examples and
marking points), or
 create an example README + Makefile for the prepaid billing project, or
 generate a set of unit tests (input files + expected outputs) you can use to auto-grade
student programs.

Which of those would you like next?

You said:

please prepare course material also for above course


ChatGPT said:

Perfect 👍 You already have a coursework plan (what to teach + labs + project). Now you
want course material — meaning lecture notes + examples + exercises module-wise, so a
student can follow along. I’ll prepare structured teaching material for each module in C
programming. You can use this as handouts, slides, or reference notes.
📘 Course Material — C Programming

Module 1: Input/Output, Decision Making & Loops


1.1 Input and Output in C

 Unformatted I/O
o printf() → output to screen
o scanf() → input from keyboard
o Format specifiers: %d (int), %f (float), %c (char), %s (string), %lf (double)

Example:

#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}

1.2 Decision Making

 if statement
 if (age >= 18) {
 printf("Eligible to vote\n");
 }
 if…else statement
 if (num % 2 == 0)
 printf("Even\n");
 else
 printf("Odd\n");
 nested if…else
 if (marks >= 75) printf("Distinction");
 else if (marks >= 50) printf("Pass");
 else printf("Fail");
 switch statement
 switch (choice) {
 case 1: printf("Tea"); break;
 case 2: printf("Coffee"); break;
 default: printf("Invalid");
 }

1.3 Looping

 while
 int i = 1;
 while (i <= 5) {
 printf("%d\n", i);
 i++;
 }
 do…while
 int i = 1;
 do {
 printf("%d\n", i);
 i++;
 } while (i <= 5);
 for
 for (int i = 1; i <= 5; i++) {
 printf("%d\n", i);
 }

1.4 Unconditional Branching

 break → exit loop


 continue → skip current iteration
 return → exit from function

Module 2: Arrays & Strings


2.1 1D Arrays
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

2.2 2D Arrays
int mat[2][2] = {{1, 2}, {3, 4}};
printf("%d", mat[1][0]); // prints 3

2.3 Strings (as char arrays)

 Without built-in functions:

int my_strlen(char s[]) {


int i = 0;
while (s[i] != '\0') i++;
return i;
}

Self-Study

 Multi-dimensional arrays
 Built-in string functions: strlen, strcpy, strcat, strcmp, strrev
Module 3: Functions & Storage Classes
3.1 Functions

 User-defined

int add(int a, int b) {


return a + b;
}

 Recursive example

int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n-1);
}

3.2 Storage Classes

 auto → default for local variables


 static → retains value between calls
 extern → refers to global variable from another file
 global → declared outside functions

Example:

void counter() {
static int count = 0;
count++;
printf("Count=%d\n", count);
}

Module 4: User-defined Functions, Structures & Unions


4.1 Functions — Call by Value vs Reference
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}

4.2 Structures
struct Student {
int roll;
char name[20];
float marks;
};

4.3 Arrays of Structures


struct Student s[3];
4.4 Unions

 Share memory among members

union Data {
int i;
float f;
char str[20];
};

Module 5: Pointers, Memory & File Handling


5.1 Pointers

 Pointer declaration:

int x = 10;
int *p = &x;
printf("%d", *p); // prints 10

 Pointer arithmetic:

int arr[3] = {10, 20, 30};


int *p = arr;
printf("%d", *(p+1)); // 20

5.2 Pointer to Structure


struct Point { int x, y; };
struct Point p1 = {10, 20};
struct Point *ptr = &p1;
printf("%d", ptr->x);

5.3 Dynamic Memory


int *arr = (int*)malloc(5 * sizeof(int));

5.4 File Handling


FILE *fp = fopen("[Link]", "w");
fprintf(fp, "Hello World");
fclose(fp);

Extra: GitHub & Project


GitHub Basics
 git init
 git add .
 git commit -m "First commit"
 git push origin main

Project — Telecom Prepaid Billing

 Data structures: subscriber info (struct)


 Features: recharge, call/data usage deduction, file-based storage
 Modules: main.c, billing.c, fileio.c
 Version control: upload to GitHub, use branches for features

📑 Self-study Topics Recap


 goto, else if ladder
 Multi-dimensional arrays
 String functions (strlen, strcpy, strcat, strcmp, strrev)
 Program design (top-down)
 Passing arrays/strings to functions
 Bit fields in structures
 Random access files (fseek, ftell)

✅ Exercises per Module


 Module 1: Print multiplication table, simple ATM simulation
 Module 2: Reverse array, check palindrome string
 Module 3: Recursive Fibonacci, static counter function
 Module 4: Student management system using structures
 Module 5: Store records in file, load them on program start

You might also like