项目作者: osalmine

项目描述 :
Hive - ft_printf
高级语言: C
项目地址: git://github.com/osalmine/ft_printf.git
创建时间: 2019-12-20T16:33:36Z
项目社区:https://github.com/osalmine/ft_printf

开源协议:

下载


ft_printf

Hive - ft_printf

Because I’m tired of using putnbr and putstr

Grade:

osalmine's 42 push_swap Score: 112/100

In this project I recoded the printf function. It creates a libftprintf.a library once compiled using:

  1. It works with the following flags:
  2. ```%c``` : prints an ascii character
  3. ```%p``` : prints the memory address of the argument
  4. ```%s``` : prints a string
  5. ```%d``` : prints a number
  6. ```%i``` : same as d
  7. ```%u``` : prints unsigned int
  8. ```%o``` : prints the number in octal (base 8)
  9. ```%x``` : prints the number in hexadecimal (base 16) with small letters
  10. ```%X``` : same as x but with big letters
  11. ```%f``` : prints floats
  12. ```%%``` : prints %
  13. See [subject](ft_printf.en.pdf) for more details.
  14. **Bonuses**
  15. ```%b``` : prints the number in binary (base 2)
  16. ```%a``` : prints a NULL-terminated 2d array (char**)
  17. It has functionality for **precision** (e.g %.3s) and **field-width** (e.g %3s). Width and precision work with * also (e.g "%.*s", 3)
  18. My ft_printf works with the following length flags:
  19. | Flags | d, i | o, u, x, X | f | a (bonus) |
  20. | ----- | ----------- | ------------------ | ------ | --------- |
  21. | h | short | unsigned short | | |
  22. | hh | signed char | unsigned char | | print strings on the same line |
  23. | l | long | unsigned long | double | |
  24. | ll | long long | unsigned long long | | |
  25. | z | size_t | size_t | | |
  26. | L | | | long double | |
  27. It works also with the following flags:
  28. **#** for o, x, X, b: value is preceeded with 0, 0x, 0X, 0b
  29. **-** : left-justify the field width
  30. **+** : forces to precede with + or -
  31. **0** : left-pads the field width with zeroes instead of spaces.
  32. **[space]** : If no sign is going to be written, insert blank before the number
  33. **Other bonuses**
  34. Colours: see ft_printf.h for full list of colours, but they can be used in the following way
  35. ```ft_printf(RED BG_BLACK "This string is in red\n" RESET );

You can also insert colours in the middle using %s:
ft_printf("%sThis is red, %sThis is blue\n" RESET, RED, BLUE );

Change the file descriptor that ft_printf prints to and can be used as such (similar to fprintf):
ft_fprintf(fd, "This string goes to different fd!\n");

Some issues:
%f flag doesn’t work with some floats with high precision (over 20), but should work with almost every.
The function isn’t super fast compared to the original printf as I did it with a complicated struct and not in bitwise.