Skip to content

Recreate printf to learn mainly how to use the variadic arguments.

Notifications You must be signed in to change notification settings

MarJC5/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

42Lausanne
Project n°3 - ft_printf

Recode printf to learn mainly how to use the variadic arguments.

Description

Recreate a basic version of the function printf(). The following conversion must be recognized cspdiuxX%.

PS: As printf() is an int function, we have to return the count of each characters that we write.

Functions needed

Step-by-step

1. Use variadic arguments

A function may be called with a varying number of arguments of varying types. The called function must declare an object of type va_list which is used by the macros va_start(), va_arg(), and va_end().

va_list will store all the argument passed to the function int ft_printf(const char *input, ...). As we can see, the ... is telling the function that it will receive variadic arguments.

Now we have setup this, we need to call the arguments, to do so va_start(args, input)is here for us.

2. Loop the input and check every characters format

As ft_printf() writes a string after converting all the arguments passed to it, we have to simplie check if the current character is a %.

  • Case 1: If the character is not a % we simply do a write of the character
  • Case 2: We found a %, then we check what current char +1 to have the variadic argument and pass it to the function void ft_printf_args(char convert, va_list args, int *rcount).

3. Proceed to the viriadic conversion

%c

This argument will simply write a character. We call the function ft_putchar_fd()and add +1 to the return *rcount.

Prototype: int ft_printf_char(int c, int *rcount)

%s

This arguments will write a string of characters. We check if the string passed to it isn't empty.

  • NULL: call ft_pustr_fd() and pass (null) to write that the current string is equal to nothing
  • Valid string: call ft_pustr_fd() to write the string

Both of the condition will count the number of characters returned with ft_strlen() and save the result to the *rcount.

Prototype: int ft_printf_str(char *str, int *rcount)

%p

This argument will print the address of a variable in hexadecimal format. To do so we convert a void *ptr to an unsigned long and convert it in hexadecimal format with the function ft_printf_hex().

Prototype: int ft_printf_ptr(void *ptr, int *rcount)

%d & %i

Both of this arguments are returning a decimal numbers in base 10. We simply use an ft_putnbr()and then print the value.

Prototype: int ft_printf_int(int nbr, int *rcount)

%u

This arguments will return a data values from zero to positive numbers. An ft_putnbr() modified with unsigned int nbr is requested as argument.

Prototype: int ft_printf_dun(unsigned int nbr, int *rcount)

%x et %X

This argument will convert any number to his hexadecimal value.

Hexadecimal numbers uses 16 values to represent a number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F.

To do so we use the same concept as ft_putnbr():

  • Divide the number by 16
  • Check if the rest is less than 10.
  • If it is, then add 48 to the rest and store the result in the array hex.
  • Otherwise, add 87 to the rest and store the result in the array hex.

The difference between %x and %X is only represended by a - f and A - F.

Prototype: int ft_printf_hex(unsigned long nbr, int *rcount, int format)

%%

This argument will write a %.

About

Recreate printf to learn mainly how to use the variadic arguments.

Topics

Resources

Stars

Watchers

Forks