whyh / ft_printf

My implementation of the printf function in C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ft_printf

My implementation of printf function in C

Replicated functional of

int printf(const char *format, ...);

Using just 6 standard C library functions

ssize_t  write(int fd, const void *buf, size_t count);
void     *malloc(size_t size);
void     free(void *ptr);
void     va_start(va_list ap, argN);
argT     va_arg(va_list ap, argT);
void     va_end(va_list ap);

ft_printf supports

  • conversions i d o u x f c s p %
  • more conversions D F X S C O U
  • flags + space - #
  • length modifiers hh h l ll L q j z t
  • precision and field width. Value of which replaced with * will be taken from arguments list, just as in ptintf

Additionally, created my own conversions

  • r requires (char*), outputs it with unprintable symbols
ft_printf("the true symbols of %r", " new line \012, tab \011, start of text \002");

  • b requires (unsigned int), outputs it in binary
ft_printf("5 in binary looks like %#b, and 9999 is definitely %b", 5, 9999);

Implemented text colouring

  • available colors white black red green yellow blue magenta cyan
  • available text transformations bold

Use [color [colorb [trsf to specify text, background color and text transformation accordingly. Color should not be followed by lowercase letters. Specify the end of formatting with ~]

ft_printf("[blueb[white Ukra~][yellowb[whiteIne  ~][green,[red[bold %s~]", "yep");

ft_printf mimics printf undefined behavior cases, such as

ft_printf("unspecified conversion %ll", 123);

ft_printf("multiple specifications %+++.1.6.f", 99.5556);

ft_printf("wrong specifications %+X", 775);

ft_printf("wrong order of specification %hh3ll17S", L"¥ ¢ £ ¤ ¶");

Benefits

My implementation is easily modifiable. To add new functionality, just add a symbol to the categorizing defines and corresponding function pointer to the array of functions

Those are the only places, that have to be modified to add new functionality (conversions, length modifiers, and flags)

How to use

Download and run withclang main.c libftprintf.a

Tested for compatibility with MacOS 10.12, Ubuntu 18.04.1

About

My implementation of the printf function in C

License:The Unlicense


Languages

Language:C 94.1%Language:Makefile 5.6%Language:CMake 0.3%