Write a library that contains ft_printf(), a function that will mimic the original printf()
_For the ft_printf() project of the 42 school cursus, we must recreate the famous C library printf() function. This project teaches us about variadic arguments. The printf() function sends a formatted string to the standard output (the display).
My ft_printf has to handle the following conversions: ↓
- %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.
libraries/
Calling for my libraries
libft/
my custom-made library ( see the project here )ft_printf.h
my ft_printf header to connect the functions created for this project
sources/
ft_printf.c
My main function implementationft_printf
the "controller"ft_translate
translate what happens after the % symbolft_putchar
write a character, of unsigned char type, to stdout (libft)ft_putnbr
function to diplay number (converted) with "ft_putchar" function (libft)ft_putnbr_unsigned
function to diplay number (unsigned int) with "ft_putchar" function (libft)ft_putptr
function print the address of pointer with "ft_puthexa" function(libft)ft_puthex
function to printf in hexadecimal format (libft)
Makefile
My build automation between libft and ft_printf libraries
This project requires GNU Compiler Collection and GNU Make compiler.
❗️| Make sure you have all the required tools installed on your local machine then continue with these steps.
0. Download the archives
Download the archives and go to the library directory:
# Clone the repository
$ git clone https://github.com/mewmewdevart/ft_printf
# Enter into the directory
$ cd ft_printf/
1. Compiling the library
Run the command in your terminal :
$ make
2. Using it in your code
To use the ft_printf actions in your code, simply include its header:
#include "libraries/ft_printf.h"
And create a main with some inserts/conversions
Example main.c
:
#include "libraries/ft_printf.h"
int main(void)
{
char *string;
string = "larissa";
ft_printf("%s ", string);
return (0);
}
If you try to compile your .c files with cc using "cc main.c" you will get an undefined symbol error for Libftprintf functions.
You have to tell the file which library it's using:
$ cc main.c libftprintf.a -o prog_example
To run the program, enter the following in the command prompt:
$ ./prog_example
The example I showed you will allow you to view a string of characters that will be displayed in your output terminal like this:
$ larissa
You can try c
, s
, p
, d
, i
, u
, x
, X
or %
conversions!
#include "libraries/ft_printf.h"
int main(void)
{
ft_printf("%d and %i is very %s", 42, 42, "cool");
return (0);
}
Output:
42 and 42 is very cool
Developed with love 💜 by Larissa Cristina Benedito (Mewmew/Larcrist).