Skip to content

Custom implementation of printf in C, handling variadic arguments and multiple format specifiers. A foundational project for understanding formatted output and string manipulation.

Notifications You must be signed in to change notification settings

MohsineDas04/ft_printf

Repository files navigation

ft_printf

🗣️ About The Project

ft_printf is a project that involves recoding the standard C library function printf. The primary goal of this project is to learn how to use variadic arguments, manage memory, and understand the intricacies of formatted output in C.

By building this library, we create a tool that can be used in future projects, replacing the standard printf with our own custom implementation.

📋 Supported Conversions

This implementation supports the following format specifiers:

Specifier Description
%c Prints a single character.
%s Prints a string (as defined by the common C convention).
%p The void * pointer argument has to be printed in hexadecimal format.
%d Prints a decimal (base 10) number.
%i Prints an integer in base 10.
%u Prints an unsigned decimal (base 10) number.
%x Prints a number in hexadecimal (base 16) lowercase format.
%X Prints a number in hexadecimal (base 16) uppercase format.
%% Prints a percent sign.

🧠 What We Learned

This project provides deep insights into several key C programming concepts:

  1. Variadic Functions (<stdarg.h>):
    • Learned how to handle functions that accept a variable number of arguments.
    • Utilized macros like va_start, va_arg, and va_end to navigate through the argument list.
  2. Low-Level I/O:
    • Used the write system call directly for output, reinforcing understanding of file descriptors and buffers.
  3. Data Representation:
    • Handled various data types (integers, characters, pointers) and converted them into string representations for display.
    • Implemented base conversion logic (decimal to hexadecimal).
  4. Static Libraries:
    • Created a library (libftprintf.a) using ar that can be linked into other programs.

🛠️ Getting Started

Prerequisites

  • GCC or Clang compiler.
  • Make.

Installation

  1. Clone the repository:
    git clone [https://github.com/yourusername/ft\_printf.git\](https://github.com/yourusername/ft\_printf.git)
    cd ft_printf

  2. Compile the library:
    make

    This will generate the libftprintf.a file.

💻 Usage

To use ft_printf in your own code, include the header file and link the library during compilation.

1. Create a main.c file:

\#include "ft\_printf.h"
int main(void)  
{  
	ft\_printf("Hello, %s\!\\n", "World");  
	ft\_printf("Character: %c\\n", 'A');  
	ft\_printf("Number: %d\\n", 42);  
	ft\_printf("Hex: %x\\n", 255);  
	ft\_printf("Pointer: %p\\n", \&main);  
	return (0);
}

2. Compile with the library:

cc main.c libftprintf.a -o test_program

3. Run the program:

./test_program

🧪 How to Test

Since ft_printf mimics the standard printf, the best way to test it is to compare its output and return value against the original.

Simple comparison test

You can create a test file that prints the same data using both functions:

\#include \<stdio.h\>  
\#include "ft\_printf.h"

int main(void)  
{  
	int ret\_ft;  
	int ret\_orig;

	// Test Strings 
	ret\_ft \= ft\_printf("My: %s\\n", "test");  
	ret\_orig \= printf("Og: %s\\n", "test");  
	printf("Return values \-\> Mine: %d | Original: %d\\n\\n", ret\_ft, ret\_orig);

	// Test Hex  
	ret\_ft \= ft\_printf("My: %x\\n", 12345);  
	ret\_orig \= printf("Og: %x\\n", 12345);  
	printf("Return values \-\> Mine: %d | Original: %d\\n", ret\_ft, ret\_orig);

	return (0);  
}

Makefile Rules

  • make: Compiles the library libftprintf.a.
  • make clean: Removes object files (.o).
  • make fclean: Removes object files and the library.
  • make re: Re-compiles the entire project.

About

Custom implementation of printf in C, handling variadic arguments and multiple format specifiers. A foundational project for understanding formatted output and string manipulation.

Topics

Resources

Stars

Watchers

Forks