Skip to content

appinha/42cursus-01-ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

42cursus' ft_printf

Development repo for 42cursus' ft_printf project
For further information about 42cursus and its projects, please refer to 42cursus repo.

GitHub code size in bytes Number of lines of code Code language count GitHub top language GitHub last commit


🗣️ About

This project is pretty straight forward. You will recode printf. Hopefully you will be able to reuse it in future project without the fear of being flagged as a cheater. You will mainly learn how to use variadic arguments.

For detailed information, refer to the subject of this project.

🚀 TLDR: this project consists of coding a library that contains a simplified version (more
information below) of the printf function.

Mandatory

  • format specifiers - cspdiuxX%
  • flags - -0.* and minimum field width with all conversions

Bonus

  • format specifiers - nfge (one or more)
  • flags - l ll h hh (one or more) and # + (all)

📑 Index

@root

@/ft_printf/

Note: files contain both mandatory and all bonus requirements.

Functions in ft_printf.c

  • ft_printf (and auxiliary static functions) - Initialization function: start/end variadic arguments functions <stdarg.h>; in between that, goes through the input string printing plain characters or collecting + treating format specifiers to the FORMAT SPECIFIER STRUCT thus printing each occurrance with the corresponding function.

Functions in ft_printf_flags.c

  • print_flags (and auxiliary static functions) - Prints each format specifier function's outputted string with formatting in accordance with the collected format specifiers.

Functions in ft_printf_csp_pct.c

The following functions output the input variable (collected by va_arg function) as a pointer to a string to be printed by the print_flags function.

  • print_spec_c - character (%c) type input variable;
  • print_spec_s - string (%s) type input variable;
  • print_spec_pct - percent (%%) character.

Functions in ft_printf_iduxo.c

The following functions output the input variable (collected by va_arg function) as a pointer to a string to be printed by the print_flags function.

  • print_spec_i_d_u - int (%i and %d) and unsigned int (%u)type input variables;
  • print_spec_x - lower (%x) and upper hexadecimal (%X) type input variables;
  • print_spec_o - octal (%o) type input variable;
  • print_spec_p - pointer (%p) type input variable.

Functions in ft_printf_f_g.c

The following functions output the input variable (collected by va_arg function) as a pointer to a string to be printed by the print_flags function.

  • print_spec_f_e_g - triage for float (%f), scientific notation (%e) and general format (%g) type input variables;
  • print_spec_f - float (%f) type input variable;
  • print_spec_g - general format (%g) type input variable.

Functions in ft_printf_f_g.c

The following functions output the input variable (collected by va_arg function) as a pointer to a string to be printed by the print_flags function.

  • print_spec_e - scientific notation (%e) type input variable.

Functions in ft_printf_utils.c

Basic Libc functions - Note: some were adapted and thus named with a suffix to indicate the nature of the customization.

  • ft_strlen - find length of string;
  • ft_strchr_01 - check the occurence of a character in a string;
  • ft_putchar_len - writes the input character to the output stream of stdout;
  • ft_putcstr_len - writes the input string to the output stream of stdout;

Functions in ft_printf_utils_2.c

Basic Libc functions - Note: some were adapted and thus named with a suffix to indicate the nature of the customization.

  • ft_strlcpy - copy string to another location;
  • ft_strlcat - concatenate strings;
  • ft_strdup - save a copy of a string (with malloc);
  • ft_strjoin - join two strings (with malloc);
  • ft_substr - extract a substring (with malloc).

Functions in ft_printf_utils_3.c

Basic Libc functions - Note: some were adapted and thus named with a suffix to indicate the nature of the customization.

  • ft_pow - returns the value of first argument raised to the power of second argument.
  • ft_ullitoa_base - allocates (with malloc) and returns a string representing the integer received as an argument according to the given base.
  • ft_fmod - returns the modulo of the float received as an argument.

Functions in ft_ftoa_rnd.c

  • ft_ftoa_rnd - allocates (with malloc) and returns a string representing the float received as an argument.

🛠️ Usage

Requirements

The library is written in C language and thus needs the gcc compiler and some standard C libraries to run.

Instructions

1. Compiling the library

To compile the library, run:

$ cd path/to/ft_printf && make

2. Using it in your code

To use the library functions in your code, simply include its header:

#include "ft_printf.h"

and, when compiling your code, add the required flags:

-lftprintf -L path/to/libftprintf.a -I path/to/ft_printf.h

📋 Testing

1. Configuring

First, open the Makefile inside /testing/ and configure the path to the ft_printf folder:

PROJECT_DIR	= ../ft_printf/

Then, configure the tests to your needs in the /testing/main.c file.

2. Running:

make

Third-party testers

📌 Useful Links