Skip to content

ft_printf is a 42 school project where we must replicate the behavior of C library printf function. The project teaches us about variadic arguments as well as data structures.

mcombeau/ft_printf

Repository files navigation

ft_printf

ft_printf 42 project badge

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 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)
  • Supported options: - 0 . * width

Status

Finished: 2022-02-07. Grade: 125/100.

Usage

make or make bonus to compile.

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.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

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.
.(*) The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

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 [%+0*i]\n", 8, 42);
    return (0);
}

Outputs:

Number [                  42]
Number [+0000042]

Made by mcombeau: [email protected] | LinkedIn: mcombeau | Website: codequoi.com

About

ft_printf is a 42 school project where we must replicate the behavior of C library printf function. The project teaches us about variadic arguments as well as data structures.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published