Assignment Overview
Functions are the building blocks of modular programming in C. In this assignment, you will create a library of useful utility functions that can be reused across multiple programs. This comprehensive project requires you to apply ALL concepts from Module 3: function declarations, definitions, prototypes, parameter passing, return values, scope, storage classes, and recursion.
<string.h> functions.
Implement strlen, strcpy, strcmp, and strrev from scratch to demonstrate your understanding.
Function Basics (3.1)
Declaration, definition, prototypes, calling conventions
Storage Classes (3.2)
auto, static, extern, register keywords
Scope and Lifetime (3.3)
Local, global, block scope, variable lifetime
Tasks
Your function library must implement ALL of the following modules. Each file will be tested individually.
Math Utilities
Create math_utils.c with the following functions:
int max(int a, int b)- Returns the larger of two numbersint min(int a, int b)- Returns the smaller of two numbersint abs_val(int n)- Returns absolute valuedouble power(double base, int exp)- Returns base raised to expint gcd(int a, int b)- Returns greatest common divisor
// math_utils.c - Example structure
#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int min(int a, int b) {
// Your implementation
}
// ... more functions
int main() {
// Test all functions
printf("max(5, 3) = %d\n", max(5, 3)); // 5
return 0;
}
Number Analysis
Create number_analysis.c with:
int is_prime(int n)- Returns 1 if prime, 0 otherwiseint is_even(int n)- Returns 1 if even, 0 if oddint digit_count(int n)- Returns number of digitsint sum_of_digits(int n)- Returns sum of all digitsint reverse_number(int n)- Returns reversed number
// number_analysis.c - Example
int is_prime(int n) {
if (n <= 1) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main() {
printf("is_prime(17) = %d\n", is_prime(17)); // 1
printf("is_prime(4) = %d\n", is_prime(4)); // 0
return 0;
}
Recursive Functions
Create recursive.c with:
int factorial(int n)- Recursive factorial calculationint fibonacci(int n)- Recursive nth Fibonacci numberint sum_to_n(int n)- Recursive sum from 1 to nvoid print_binary(int n)- Print number in binary recursively
// recursive.c - Example
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
void print_binary(int n) {
if (n > 1) print_binary(n / 2);
printf("%d", n % 2);
}
String Utilities (without string.h)
Create string_utils.c with custom implementations:
int str_length(char str[])- Returns string lengthvoid str_reverse(char str[])- Reverses string in placeint str_compare(char s1[], char s2[])- Compares two stringsvoid str_copy(char dest[], char src[])- Copies source to destination
<string.h> - implement all string operations from scratch!
// string_utils.c - Example
int str_length(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main() {
printf("Length: %d\n", str_length("hello")); // 5
return 0;
}
Main Driver Program
Create main.c that:
- Demonstrates all functions from all utility files
- Implements a menu system for user to test different utilities
- Handles user input validation gracefully
- Uses proper function prototypes in a header file or at top of main.c
// main.c - Menu system example
#include <stdio.h>
// Function prototypes
int max(int a, int b);
int is_prime(int n);
int factorial(int n);
int str_length(char str[]);
int main() {
int choice;
printf("=== Function Library ===\n");
printf("1. Math Utilities\n");
printf("2. Number Analysis\n");
printf("3. Recursive Functions\n");
printf("4. String Utilities\n");
printf("5. Exit\n");
printf("Choice: ");
scanf("%d", &choice);
// Handle menu selection...
return 0;
}
Submission
Create a public GitHub repository with the exact name shown below:
Required Repository Name
c-function-library
Required Files
c-function-library/
├── math_utils.c # 5 math utility functions
├── number_analysis.c # 5 number analysis functions
├── recursive.c # 4 recursive functions
├── string_utils.c # 4 string utility functions
├── main.c # Driver program with menu system
└── README.md # REQUIRED - see contents below
README.md Must Include:
- Your full name and submission date
- Brief description of each utility file and its functions
- Compilation instructions (
gcccommands) - Any challenges faced and how you solved them
Do Include
- All 18+ functions implemented and working
- Comments explaining complex logic
- Test cases in main() for each function
- Proper function prototypes
- Clean, indented, readable code
- README.md with all required sections
Do Not Include
- Compiled binaries (.exe, .out files)
- Object files (.o files)
- IDE-specific folders (.vscode, .idea)
- Code that does not compile without errors
- Iterative solutions for recursive tasks
- string.h usage in string_utils.c
gcc -o main main.c math_utils.c number_analysis.c recursive.c string_utils.c
Enter your GitHub username - we'll verify your repository automatically
Grading Rubric
Your assignment will be graded on the following criteria:
| Criteria | Points | Description |
|---|---|---|
| Math Utilities | 20 | All 5 functions work correctly with various inputs |
| Number Analysis | 20 | All 5 functions work correctly, including edge cases |
| Recursive Functions | 25 | Proper recursion used (not iteration), correct base cases |
| String Utilities | 20 | Custom implementations without string.h, handles edge cases |
| Main Driver | 15 | Menu system works, proper testing, input validation |
| Total | 100 |
Ready to Submit?
Make sure you have completed all requirements and reviewed the grading rubric above.
Submit Your AssignmentWhat You Will Practice
Function Design (3.1)
Writing modular functions with proper declarations, definitions, prototypes, parameters, and return values
Storage Classes (3.2)
Understanding auto, static, extern, and register keywords for controlling variable storage and visibility
Scope and Lifetime (3.3)
Managing local vs global variables, block scope, and understanding when variables are created and destroyed
Recursive Thinking
Breaking problems into base cases and recursive cases, understanding call stack behavior
Pro Tips
C Best Practices
- Use meaningful function and variable names
- Always declare function prototypes
- Handle edge cases (negative numbers, zero, empty strings)
- Test functions with multiple inputs
Recursion Tips
- Always define a clear base case first
- Ensure recursive calls move toward base case
- Trace through small examples on paper
- Watch for stack overflow with large inputs
Time Management
- Start with the simplest functions first
- Build and test incrementally
- Leave time for the menu driver program
- Test compilation frequently
Common Mistakes
- Forgetting to handle negative numbers in digit functions
- Using loops instead of recursion
- Missing null terminator in string operations
- Integer overflow in factorial for large n