My implementation of printf
function
42 algorithmic project. The code is written in accordance with The Norm (42 coding style).
This repo uses my own C library as a submodule.
- Clone this repo, you will have empty
libft
folder then - Run
git submodule init
andgit submodule update
, after that you will have relevantlibft
state - Run
make
in this repo folder, this will compilelibft
andprintf
files tolibftprintf.a
static library main.c
contains some of my tests, to compile it run script:bash run.sh
(you can add/change files you want to compile inrun.sh
Makefile
make
-- compileslibftprintf.a
.clean
-- deletes object files.fclean
-- deletes object files andlibftprintf.a
.re
-- runsfclean
and recompiles.
This version supports c s p d i u x X %
conversions, - 0
flags, width and precision fields (also passed via an asterisk).
This version reproduces undefined behavior which may occur in combinations of the above mentioned flags and specifiers.
Return value is number of written bytes or -1 in case of write
error.
In case of invalid specifier (e.g. printf("invalid %10W", 21)
, W
- invalid) my function writes this invalid specifier as a simple character (so output will be invalid W
).
printf("format", [arguments ...])
"format" = %[flags][width][.precision]type
- - the value is leftaligned within the specified width; by default, it is rightaligned
- 0 adds zeros within specified width, by default - spaces
- width minimum field width
- precision
- d, i signed decimal (int)
- u unsigned decimal (unsigned int)
- x, X unsigned hexadecimal notation (unsigned int converted to hex)
- c int argument converted to unsigned char
- s pointer to string
- p pointer of void type
- % a % character is written
behavior depends on type (see below)
can be specified directly in
format
(nonnegative decimal integer) or through *
(int argument)negative width is taken as a
-
flag followed by a positive width (both for format
and *
)if value length > width, width is ignored, otherwise it is padded with spaces on the left (or depending on flags)
.
followed either by nonnegative decimal integer or through *
(int argument)if only the
.
is specified, the precision is taken as zerobehavior depends on type (see below)
if
0
, leading zeros (if negative, sign before zeros) are used to pad to the widthif
0
and -
both appear, the 0
is ignoredif a precision is specified, the
0
is ignoredprecision - minimum number of digits to appear (if value < precision, expanded with zeros)
the result of converting a 0 with 0 precision is no characters
similar to d, i
x uses lower case (abcdef), X - upper case (ABCDEF)
similar to d, i
undefined behavior with
0
and specified precision
undefined behavior with
0
precision specify the maximum number of bytes to be written
undefined behavior with
0
and specified precision
undefined behavior with
0
and specified precision