A custom partial implementation of the printf function from C standard library
Welcome to ft_printf, a custom PARTIAL implementation of the famous printf function in C. This project is designed to help you learn and understand C programming, variadic functions, and formatting output.
ft_printf is a function that formats and prints data to the standard output. It supports various format specifiers, flags, and data types, making it a versatile tool for displaying information in your C programs.
int ft_printf(const char *format, ...); (manual)
Variadic functions are functions that can take a variable number of arguments. The standard C library function printf is a well-known example of a variadic function.
Variadic functions can be declared with an ellipsis (...) as the last parameter in the parameter list, indicating that the function accepts a variable number of arguments. To handle the arguments passed to a variadic function, the C standard library provides a set of macros in the stdarg.h header.
The following macros are used to handle variadic function arguments:
-
va_list: A type used to declare a variable that will store the argument list. -
va_start(ap, param): Initializes theva_listobjectapto start reading arguments after the last named parameterparam. -
va_arg(ap, type): Retrieves the next argument from theva_listobjectapwith the specifiedtype. -
va_end(ap): Cleans up theva_listobjectapafter all arguments have been read.
| Specifier | Description |
|---|---|
| %c | Character |
| %s | String |
| %d or %i | Signed decimal integer |
| %u | Unsigned decimal integer |
| %x | Unsigned hexadecimal integer (lowercase letters) |
| %X | Unsigned hexadecimal integer (uppercase letters) |
| %p | Pointer address |
| %% | A literal '%' character |
When implementing ft_printf, it is important to consider the following edge cases:
-
Null pointers passed as string arguments (
%s): In this case, the function should print(null). -
Negative numbers for signed integer formats (
%dand%i): The function should correctly handle the sign and the conversion of the number. -
Large numbers and edge values: The function should be able to handle the largest and smallest representable values for each data type (e.g.,
INT_MIN,INT_MAX,UINT_MAX, and pointer addresses). -
Incorrect or unsupported format specifiers: The function should be able to handle unexpected or unsupported format specifiers gracefully.
To use ft_printf, compile the source files and include the header ft_printf.h in your project. You can then use ft_printf just like you would use the standard printf function.