Skip to content

[42 Cursus] Because ft_putnbr() and ft_putstr() aren’t enough

License

Notifications You must be signed in to change notification settings

mewmewdevart/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_printf

Write a library that contains ft_printf(), a function that will mimic the original printf()

GitHub code size in bytes Main language License

💡 About the project

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

📁 Files/

  • 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 implementation
    • ft_printf the "controller"
    • ft_translate translate what happens after the % symbol
      • ft_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

🛠️ Usage

Requirements

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.

Instructions

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

📋 Credits

Developed with love 💜 by Larissa Cristina Benedito (Mewmew/Larcrist).

About

[42 Cursus] Because ft_putnbr() and ft_putstr() aren’t enough

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published