Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Review Assignment Due Date

Assignment: Custom printf() Function

Introduction

Your task is to create a custom implementation of printf to handle format specifiers c, s, d, i, b, %, and nonspecifier.

Starter Files

Create a new directory, custom_printf/.

Project Structure Example

custom_printf/
├── main.c //implementation of custom_printf
├── custom_printf.c
├── custom_printf.h
├── README.md
└── flow_chart.png

No garbage files such as DS_Store, .vscode, .o files, etc.

Compilation Instructions

To compile the project, use the following command:

gcc -Wall -Werror -Wextra -pedantic -std=c11 -o main main.c source_file.c

Ensure there are no warnings or errors when compiling.

valgrind -s --leak-check=full --show-leak-kinds=all --track-origins=yes output_file

Memory should be properly allocated and freed to prevent leaks.

Requirements

Variadic Function

  • 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-case statements.
  • Function should include basic error handling to control for null arguments.
  • 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 and Expected 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

Submission Requirements

  • 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

Grading Criteria

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.).

Conclusion

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages