This project is a custom implementation of the printf
function in C, which is used for formatted output. The standard printf
function in C is powerful but complex, and implementing it from scratch is an excellent exercise in C programming, string manipulation, memory management, and handling variadic functions.
- Features
- Requirements
- Installation
- Usage
- Supported Format Specifiers
- Examples
- Testing
- Contributing
- Authors
- License
- Acknowledgements
- Custom implementation of the
printf
function - Handles various format specifiers
- Manages different data types
- Supports width and precision formatting
- Handles flags for formatting options
- GCC compiler
- C standard library
- Make (optional, for building with Makefile)
Clone the repository to your local machine:
git clone https://github.com/Adameelmadani/printf.git
cd printf
Compile the project:
make
Or compile manually:
gcc -Wall -Werror -Wextra -pedantic *.c -o printf
Include the header file in your C program:
#include "main.h"
Use the custom _printf
function similar to how you would use the standard printf:
_printf("Hello, %s! The answer is %d.\n", "world", 42);
The implementation supports the following format specifiers:
%c
: Character%s
: String%d
or%i
: Signed decimal integer%u
: Unsigned decimal integer%o
: Unsigned octal%x
: Unsigned hexadecimal integer (lowercase)%X
: Unsigned hexadecimal integer (uppercase)%p
: Pointer address%%
: Percent sign
Here are some examples of how to use the custom printf function:
#include "main.h"
int main(void)
{
_printf("Character: %c\n", 'H');
_printf("String: %s\n", "Hello, world!");
_printf("Integer: %d\n", 42);
_printf("Negative integer: %i\n", -42);
_printf("Unsigned: %u\n", 42);
_printf("Octal: %o\n", 42);
_printf("Hexadecimal (lowercase): %x\n", 42);
_printf("Hexadecimal (uppercase): %X\n", 42);
_printf("Percent sign: %%\n");
_printf("Pointer: %p\n", (void *)0x7ffe637541f0);
return (0);
}
To test the implementation, you can use the provided test files:
make test
Alternatively, you can create your own test files to verify specific functionality.
printf/
├── _printf.c # Main printf implementation
├── main.h # Header file with function prototypes
├── handle_char.c # Functions for character handling
├── handle_string.c # Functions for string handling
├── handle_integer.c # Functions for integer handling
├── handle_binary.c # Functions for binary conversion
├── handle_octal.c # Functions for octal conversion
├── handle_hex.c # Functions for hexadecimal conversion
├── utils.c # Utility functions
├── Makefile # Compilation instructions
└── test/ # Test files
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - MIT License
- The C Programming Language by Brian Kernighan and Dennis Ritchie
- printf(3) - Linux manual page