Your task is to create a custom implementation of printf to handle format specifiers c, s, d, i, b, %, and nonspecifier.
Create a new directory, custom_printf/.
custom_printf/
├── main.c //implementation of custom_printf
├── custom_printf.c
├── custom_printf.h
├── README.md
└── flow_chart.pngNo garbage files such as DS_Store, .vscode, .o files, etc.
To compile the project, use the following command:
gcc -Wall -Werror -Wextra -pedantic -std=c11 -o main main.c source_file.cEnsure there are no warnings or errors when compiling.
valgrind -s --leak-check=full --show-leak-kinds=all --track-origins=yes output_fileMemory should be properly allocated and freed to prevent leaks.
- Implement your own custom version of
printf()that is a variadic function able to handle format specifiers. - You may use standard C libraries such as <stdio.h>, <stdlib.h>, <stdarg.h>, and <unistd.h>.
- Function should be able to handle format specifiers
c,s,d,i,b,%, and nonspecifier. - Invalid format specifiers should be ignored.
- No use of
switch-casestatements. - Function should include basic error handling to control for
nullarguments. - Function should be named
customPrintf(). - Returns the total number of characters printed.
- Uses dynamic memory allocation.
- Complies with strict compilation flags to enforce clean and error-free code.
- Memory allocation must be used when necessary, particularly for handling formatted output.
Sample Function Calls:
customPrint("Character: %c", 'A');
//remember, we have to use the standard printf for a newline because it doesn't exist in our custom version!
printf("\n");
customPrint("String: %s", "Hello, World!");
printf("\n");
customPrint("Integer: %d", 42);
printf("\n");
customPrint("Binary: %b", 5);
printf("\n");
customPrint("Percent sign: %");
printf("\n");
customPrint("Nonspecifier");
printf("\n");
customPrint(NULL);Sample Output:
Character: A
String: Hello, World!
Integer: Decimal: 42
Binary: 101
Percent sign: %
Nonspecifier
Error: formatString is NULL-
Well-documented ReadMe including
- Flowchart visualization of your program
- Description of Projects
- Group Members
- How to Run projects
- Project structure breakdown
- Explanations
- What was not completed or what would you run
- Any error, run, or build issues
- Environment code built and run successfully on
-
Well-structured code following best practices.
-
Ensure all necessary files are included and no extra or unnecessary files are present
| Criteria | Percentage | Description |
|---|---|---|
| Correctness | 40% | Does your printf function correctly format and print output? |
| Memory Management | 20% | Proper use of memory allocation and deallocation. |
| Code Structure & Readability | 20% | Is your code well-organized and readable? |
| Compilation & Warnings | 10% | No warnings or errors when compiled with the specified flags. |
| Garbage Files | -5 points | Deducted per unnecessary file (e.g., DS_Store, .vscode, .o files, etc.). |
In this exercise, we successfully learned how to implement our own printf variadic function! We also implemented basic error handling of null parameters, handling a myriad of format specifiers, and handling the condition where there is no format specifier presented.