pgomez-a / ft_printf

The definition of this project is simple: replicate the workings of the famous printf function.

Home Page:https://www.linkedin.com/in/pgomez-a/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ft_printf

ft_printf

If you want to learn more about IT topics, I invite you to join my Patreon channel and visit my website: IA Notes

The definition of this project is simple: replicate the workings of the famous printf function. Thanks to this, it won't be necessary for us to continue using ft_putchar_fd and ft_putstr_fd every time we want to display a character, a string of characters or a number on the screen.

MAIN OBJECTIVES

The main objective that we want to achieve with this project is to be able to define functions that receive a variable number of arguments. Up to now, every time we define the prototype of a function, we establish a series of mandatory (fixed) parameters that must be entered when calling the function. However, we can see that a function like printf can receive as many parameters as the user specifies. So how is it able to do this? The simple answer is found in the use of the <stdarg.h> library.

HOW TO USE FT_PRINTF?

  1. Clone ft_printf repository:

    git clone https://github.com/pgomez-a/ft_printf.git && cd ft_printf
    
  2. Run make to create the static library libftprintf.a:

    make
    
  3. Now, you can move the library wherever your project is, but the include of your main project should be the path of the ft_printf repository:

    /* Suppose main.c is in the ft_printf repository */
    
    #include "ft_printf.h"
    
    int main(void)
    {
    
  4. Compile your program with the new library:

    gcc main.c -L. -lftprintf
    

<stdarg.h>

This library will allow us to work with the use of functions capable of receiving a variable number of arguments. To do this, you will declare a new type of variable and three different macros:

  • va_list --> variable
  • va_start --> macro
  • va_arg --> macro
  • va_end --> macro

If you want to know more about how to use each of these macros, you already know the answer: RTFM. However, if you want to ask me a question or discuss about some functionality of this library, you can do it through the contact information of my README: I'll be happy to talk to you !!

RETURN VALUES

Although the use of printf consists of displaying a format string on the standard output (stdin;), it also has a return value, since it is a function of type int. This return will be the number of characters you have printed on the screen, and not the number of characters in the format string.

EXAMPLES OF USE

Some examples of the use of our ft_printf, comparing it with the real printf and being ft = output_printf and sy = output_ft_printf:

Regular case main Regular example main

Regular case main Regular example main

However, there are some particular cases that we must take into account, both when working with the computer memory and when making a correct cast:

Special case main.c Special case example

About

The definition of this project is simple: replicate the workings of the famous printf function.

https://www.linkedin.com/in/pgomez-a/

License:MIT License


Languages

Language:C 96.2%Language:Makefile 3.8%