Skip to content

About

This project belongs to 42 milestone1, which tries to reproduce the behaviour of built in function printf() in C language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

This project has been created as part of the 42 curriculum by sliang

Description

This project belongs to 42 milestone1, which tries to reproduce the behaviour of built in function printf() in C language. The mandatory part requires the function handles conversions: cspdiuxX%.

In my ft_printf main function part, it contains 1 helper function: print_conversoin. This part will look for the character after % as format specifier. Each character tells the function prints out different data type, and return how many bytes it prints.

conversion Instruction
%c Prints a single character
%s Prints a string
%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.

In print_conversion function, there are three functions handle all cases.

conversion Instruction
ft_putchr only print one byte with write()
ft_putstr print byte with write() one by one in while loop while string is not arrive NULL terminator
ft_putnbr handle digit input including integers
ft_putnbr_unsigned handle digit input including unsigned intergers, hexadecimal integers
ft_putptr handle pointers, which will be used for printing memory address in hexademical format, call ft_putnbr_unsigned inside

In ft_putnbr function, number, base (10-digit or 16 digit base) and symbols (lowercase or uppercase) are passed as arguments. This function check three conditions.

  • If the number is negative, it prints "-" first and convert number as postive to handle.
  • If the number is less than base (only one byte), it prints out the number.
  • If the number is outside of the base, in other words, more than two bytes to print, it goes into recursion session to distract digit and print from left to right.

The reason to create ft_putnbr_unsigned function is to avoid data reinterpretation when passing different data types across different functions.

Instruction

Here is an overview how to run this program with C compiler and Bash terminal.
After download the program directory, you can run make to archive ft_printf.

make

libftprintf.a is ready and you can compile with your main.c file.

cc main.c libftprintf.a

Now, you can run the output file! For example

./a.out

If you want to clean object files

make clean

If you want to clean object files and library file

make fclean

If you want to re-make the library file

make re

Resuorces

YouTube video -- Let's build a mini printf function
article -- Variadic Functions in C
article -- va_list in C: exploring ft_printf
C data types

Other notes

This project only use AI to explain logic of codes. No code is generated by AI.

About

This project belongs to 42 milestone1, which tries to reproduce the behaviour of built in function printf() in C language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages