The purpose of this project is to recode C's original function printf().
This library reimplements the core functionality of the standard printf()
and fprintf()
functions. It also implements the ft_eprintf()
function.
Features:
- Full variadic argument support: handles variable number of arguments using
va_list
- Comprehensive format specifiers: supports all basic printf conversion specifiers
- Error handling: proper handling of malformed format strings
Supported Format Specifiers:
Specifier | Type | Description |
---|---|---|
%c |
Character | Prints a single character |
%s |
String | Prints a null-terminated string |
%d |
Integer | Prints a signed decimal integer |
%i |
Integer | Prints a signed decimal integer (same as %d) |
%u |
Unsigned Integer | Prints an unsigned decimal integer |
%o |
Octal | Prints an unsigned integer in octal base |
%x |
Hexadecimal | Prints an unsigned integer in lowercase hexadecimal |
%X |
Hexadecimal | Prints an unsigned integer in uppercase hexadecimal |
%p |
Pointer | Prints a pointer address in hexadecimal format |
%% |
Literal | Prints a literal '%' character |
int ft_printf(char const *format, ...);
int ft_fprintf(int fd, char const *format, ...);
int ft_eprintf(char const *format, ...);
Parameters:
format
: Format string containing text and format specifiers...
: Variable arguments corresponding to format specifiersfd
(forft_fprintf
): File descriptor to which output is written
Return value:
- Number of characters printed on success
-1
on error (malformed format string)
For detailed info, refer to this project subject.
- GCC compiler
- Make utility
- Unix-like system (Linux, macOS, WSL)
-
Debian/Ubuntu
sudo apt install build-essential
-
Clone the repository:
git clone https://github.com/sdevsantiago/ft_printf.git cd ft_printf
-
Compile the library:
make # Full compilation make clean # Remove object files make fclean # Remove all generated files make re # Rebuild everything from scratch
-
Clean build files:
make clean # Remove object files make fclean # Remove all generated files make re # Rebuild everything from scratch
Command | Description |
---|---|
make |
Compiles the ft_printf library (creates libftprintf.a) |
make all |
Same as make |
make clean |
Remove object files (*.o) |
make fclean |
Remove object files and the library |
make re |
Clean and rebuild everything |
-
Include the header in your C file:
#include "ft_printf.h"
-
Compile your program with the library:
cc -Wall -Wextra -Werror your_file.c path/to/ft_printf/libftprintf.a -I path/to/ft_printf/ -o your_program
#include "ft_printf.h"
#include <fcntl.h>
int main(void)
{
// Basic examples
ft_printf("Hello, World!\n");
ft_printf("Character: %c\n", 'A');
ft_printf("String: %s\n", "42 School");
ft_printf("Number: %d\n", 42);
ft_printf("Unsigned: %u\n", 3000000000U);
ft_printf("Hexadecimal: %x %X\n", 255, 255);
ft_printf("Octal: %o\n", 64);
ft_printf("Pointer: %p\n", &main);
ft_printf("Percentage: %%\n");
// Mixed format specifiers
ft_printf("User %s has %d points and %u coins\n", "Alice", -50, 1500);
// Hexadecimal formatting
ft_printf("Memory address: %p, Value in hex: 0x%x\n", ptr, value);
// Error handling
int chars_printed = ft_printf("%", 42);
if (chars_printed == -1)
{
// Handle error
}
int fd = open("file", O_WRONLY);
if (fd != -1)
{
ft_fprintf(fd, "Hello %d\n", 42);
}
else
{
ft_eprintf("error: unable to write on file\n");
}
return (0);
}
The code strictly complies with 42's Norminette v4:
norminette *.c *.h
More info in the official Norminette repository.
- lrcouto and ayogun for creating and publishing, respectively, the 42-project-badges repository.
- gcamerli for creating the 42unlicense repository.
This work is published under the terms of 42 Unlicense. This means you are free to use, modify, and share this software.