Restricted implementation of the printf function from the standard C library.
The prototype is int ft_printf(char *format, ...)
where format
is the format string that specifies how subsequent optional arguments are converted for output.
The format string is composed of zero or more directives: either ordinary characters that are simply copied to output or conversion specifications that start with an %
and consist of the following:
%[flags][minimum field width][.precision][length modifier][conversion type]
Flags, minimum field width, precision and length modifier are optional. At its simplest, the conversion specification is of the form %[conversion type]
, e.g. %d
. %%
produces the % character.
Conversion type | Length modifier | Precision | Min. field width | Flags |
---|---|---|---|---|
d , i : signeddecimal |
default : inth : shorthh : signed charl : longll : long long |
minimum number of digits; if only . is given, precision is taken as zero; if fewer digits are needed, the value is padded on the left with zeroes |
if the converted value, with flags considered, has fewer characters than the field width (given as a decimal digit string), it will be padded with spaces on the left (or right, if the - flag has been given) to fill out the given field width. |
+ add a plus sign in front of a non-negative number' ' add a blank in front of a non-negative number; ignored if + is given0 zero padding: padding on the left with '0's instead of blanks; ignored if precision or - is given- negative field width: values left-adjusted |
o : unsignedoctal |
default : unsigned inth : unsigned shorthh : unsigned charl : unsigned longll : unsigned long long |
-"- | -"- | # precision is increased (if needed) to force the first digit to be 00 zero padding; ignored if precision or - is given- negative field width |
u : unsigneddecimal |
-"- | -"- | -"- | 0 zero padding; ignored if precision or - is given- negative field width |
x , X : unsignedhexadecimal |
-"- | -"- | -"- | # non-zero result gets prefix "0x" or "0X"0 zero padding; ignored if precision or - is given- negative field width |
c : char (intconverted to unsigned char) |
default : int |
- | -"- | - negative field width |
s : characterstring (pointer to char array) |
default : char* |
maximum number of characters to print; if precision is specified and is less than the length of the array, the string does not have to be nul-terminated | -"- | - negative field width |
f , F : double |
default : doublel : doubleL : long double |
number of digits to appear after the decimal point; if precision is zero as in %.f or %.0f , only the integral part of the double will be printed; if precision is omitted, it is set to a default value of 6 |
-"- | # result contains a decimal point even if precision is set to zero+ add a plus sign in front of a non-negative number' ' add a blank in front of a non-negative number; ignored if + is given0 zero padding; ignored if - is given- negative field width |