Skip to content

The goal of this project is pretty straightforward. You will recode printf(). You will mainly learn about using a variable number of arguments. How cool is that?? It is actually pretty cool :)

License

Notifications You must be signed in to change notification settings

itsmeodx/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_printf

The goal of this project is pretty straightforward. You will recode printf(). You will mainly learn about using a variable number of arguments. How cool is that?? It is actually pretty cool :)

Introduction

For this project of the 42 school cursus, we must recreate the famous C library printf function. This project teaches us about variadic arguments as well as structures if we plan to implement printf's extra flags.

  • Supported conversions: %, c, s, p, i, d, u, x, X
  • Supported flags: # + - (space) 0
  • Supported options: . width

Status

Finished: 2024-02-04. Grade: 125/100.

Usage

make to compile libftprintf.a, a library that contains ft_printf functions and it's dependencies (including Libft).

Basic Usage

For example, let's create a main.c file.

// Include the header
#include "ft_printf.h"

int main(void)
{
      // Call the function
      ft_printf("Testing ft_printf!");
      return (0);
}

Compile the main.c file with the ft_printf library and run the program:

gcc main.c libftprintf.a && ./a.out

Output should be:

Testing ft_printf!

Advanced Usage: Format Specifiers

This ft_printf function supports several format specifiers, described below. These are optional, can be used in any combination, and are implemented as: %[flags][width][.precision]specifier

The table below lists supported format specifiers:

Flags

Flag Description
- Left justify the result within the field. By default it is right justified.
+ Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a sign.
(space) If there is no sign, a space is attached to the beginning of the result.
# Used with x or X specifiers the value is preceded with 0x or 0X respectively for values different than zero.
0 Leading zeros are used to pad the numbers instead of space.

Width

Value Description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

Precision

Value Description
.(number) For integer specifiers (d, i, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For s − this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

Specifiers

Format Specifier Description
% % followed by another % character writes % to the screen.
c writes a single character.
s writes a character string.
p writes an implementation-defined character sequence defining a pointer address.
d or i writes a signed integer to decimal representation.
u writes an unsigned integer to decimal representation.
x or X writes an unsigned integer to hexadecimal representation.

For example, let's ask to print the integer 42 with a width of 20, and then to print it again but with a width of 8, the 0 flag and the + flag:

#include "ft_printf"

int main(void)
{
    ft_printf("Number [%20i]\n", 42);
    ft_printf("Number [%+08i]\n", 42);
    return (0);
}

Outputs:

Number [                  42]
Number [+0000042]

ODX

About

The goal of this project is pretty straightforward. You will recode printf(). You will mainly learn about using a variable number of arguments. How cool is that?? It is actually pretty cool :)

Resources

License

Stars

Watchers

Forks