This project was made in accordance with the project of School 21 (Ecole 42).
The purpose of this project is to recode printf function in c.
You can see the subject here: ft_printf.
Main requirements, rules and code style: Norm.
The libft
subdirectory is another project for School 21: Libft.
-
The
printf()
function produces output according to a format as described below.
The format string is a character string, beginning and ending in its initial shift state, if any.
The format string is composed of zero or more directives: ordinary characters (not%
), which
are copied unchanged to the output stream; and conversion specifications, each of which results
in fetching zero or more subsequent arguments. Each conversion specification is introduced by
the character%
, and ends with a conversion specifier. In between there may be (in this order)
zero or more flags, an optional minimum field width, an optional precision and an optional length modifier. -
The overall syntax of a conversion specification is:
%[flags][width][.precision][length modifier][conversion]
ft_printf()
must process following type specifiers:
-
%c
- single character -
%s
- string of characters -
%p
- pointer to void (in hexadecimal) -
%d
- decimal number -
%i
- integer number -
%u
- unsigned decimal number -
%x
- number in hexadecimal (lowercase) -
%X
- number in hexadecimal (uppercase) -
%%
- percent character
ft_printf()
must manage following format specifiers and minimum field width in any combination:
-
-
- left-justify within the given field width, right justification is the default -
0
- left-pads the number with zeroes instead of spaces, where padding is specified -
.
- precision (is used with numeric values) -
#
- adding prefix0x
or0X
for%x
or%X
type specifiers respectively -
+
- adding sign (+
or-
) in dependency of output value. By default shows only-
sign
for negative values -
-
sign
for negative values. Ignored if both+
and
-
Also you can use width specifier (number) to define the field width.
-
Width and precision specifiers can be presented due to
*
character.
In this case*
should be printed in formatting string for ft_printf at required position instead of
numeric value. At the same time value for*
is passed as argument. -
For futher information about
printf()
read the manual or wiki.
_
is showing spaces in output.
Command | Output |
---|---|
ft_printf("%3d", 1) | __1 |
ft_printf("%3d", 123456789) | 123456789 |
ft_printf("%3d", -1) | _-1 |
ft_printf("%-3d", 1) | 1__ |
ft_printf("%-3d", 123456789) | 123456789 |
ft_printf("%-3d", -1) | -1_ |
ft_printf("%03d", 1) | 001 |
ft_printf("%03d", 123456789) | 123456789 |
ft_printf("%03d", -1) | -01 |
ft_printf("%+5d", 10) | __+10 |
ft_printf("%-+5d", 10) | +10__ |
ft_printf("%s", "Hello") | Hello |
ft_printf("%10s", "Hello") | _____Hello |
ft_printf("%-10s", "Hello") | Hello_____ |
Makefile compiles given functions into C static library file.
Compiler: gcc
Flags: -Wall
-Werror
-Wextra
- Go to the project folder:
$ cd 'path_to_ft_printf'
- Then typo one of these command:
Command | Description |
---|---|
make |
compiling mandatory and bonus part |
make bonus |
compiling mandatory and bonus part |
make clean |
clearing all .o files |
make fclean |
clearing all .o files and library |
-
As you can see
make
andmake bonus
have the same behavior. -
To use compiled project in your code just include library header:
#include "ft_printf.h"
- While compiling your code add these flags:
-lft -L 'path_to_libftprintf.a' -I 'path_to_ft_printf.h'
-
You can check code norm due to norminette.
-
These are some testers for
ft_printf
project: