Giter Site home page Giter Site logo

ft_printf's Introduction

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 %.

ft_printf's People

Contributors

marjc5 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.