/ft_printf

Implementation of my own printf function from stdio.h

Primary LanguageC

ft_printf

A recreation of the standard C library's printf function.

Function prototypes:

int		ft_printf(const char *format, ...)
char	*ft_sprintf(char *s, const char *format, ...)
int		ft_dprintf(int fd, const char *format, ...)

Usage:

Run make, which results in library called libftprintf.a. In your source file, use ft_printf() function just as you would use printf(). Furthermore, compile your source code with libftprintf.a to use ft_printf() function. So libftprint.a includes the ft_printf() function.

gcc main.c libftprintf.a

Syntax for format specifiers:

%[flags][width][.precision][length]type

This implementation of printf supports the following:

Flags field
#   (hash)
0   (zero)
  (space)
+   (plus)
-   (minus)
Width field
<number>
*
Precision field
.<number>
.*
Length field
hh
h
l
ll
j
z
Type field
d or i
D
o
O
u
U
x
X
p
c
C
s
S
%
b
r

The format specifiers %b is custom. They print binary, respectively.

Example:

#include "ft_printf.h"

int    main(void)
{
    int i;

    setlocale(LC_ALL, "");
    ft_printf("Hello World!\n");
    ft_printf("%s", "This is ");
    ft_printf("%.*s\n", 9, "ft_printf and ft_dprintf");
    ft_printf("%d\n", 123);
    ft_printf("%05d\n", 42);
    ft_printf("%+09d\n", 42);
    ft_printf("%u\n", UINT_MAX);
    ft_printf("%#b\n", 256);
    ft_printf("%#o\n", 1039);
    ft_printf("%#X\n", 16417188);
    ft_printf("%p\n", &i);
    return (0);
}

Output:

Hello World!
This is ft_printf
123
00042
+00000042
4294967295
0b100000000
02017
0XFA81A4
0x7fff5c7159e8