Skip to content

cgriceld/42-printf-C

Repository files navigation

printf

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.

How it works:

  1. Clone this repo, you will have empty libft folder then
  2. Run git submodule init and git submodule update, after that you will have relevant libft state
  3. Run make in this repo folder, this will compile libft and printf files to libftprintf.a static library
  4. main.c contains some of my tests, to compile it run script: bash run.sh (you can add/change files you want to compile in run.sh

Makefile

  • make -- compiles libftprintf.a.
  • clean -- deletes object files.
  • fclean -- deletes object files and libftprintf.a.
  • re -- runs fclean 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).

Some theory:

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
    behavior depends on type (see below)
  • width
  • minimum field width
    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)
  • precision
  • . followed either by nonnegative decimal integer or through * (int argument)
    if only the . is specified, the precision is taken as zero
    behavior depends on type (see below)
  • d, i
  • signed decimal (int)
    if 0, leading zeros (if negative, sign before zeros) are used to pad to the width
    if 0 and - both appear, the 0 is ignored
    if a precision is specified, the 0 is ignored
    precision - minimum number of digits to appear (if value < precision, expanded with zeros)
    the result of converting a 0 with 0 precision is no characters
  • u
  • unsigned decimal (unsigned int)
    similar to d, i
  • x, X
  • unsigned hexadecimal notation (unsigned int converted to hex)
    x uses lower case (abcdef), X - upper case (ABCDEF)
    similar to d, i
  • c
  • int argument converted to unsigned char
    undefined behavior with 0 and specified precision
  • s
  • pointer to string
    undefined behavior with 0
    precision specify the maximum number of bytes to be written
  • p
  • pointer of void type
    undefined behavior with 0 and specified precision
  • %
  • a % character is written
    undefined behavior with 0 and specified precision

About

reinventing the wheel : recode printf

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages