Creating printf from scratch
A formatted output conversion C program completed . The program is a recreation of the C standard library function printf from scratch.
- Prototype:
int _printf(const char *format, ...);
To use the _printf
compile all .c
files in the repository and include the header holberton.h
with any main function.
Example main.c
:
#include "holberton.h"
int main(void)
{
_printf("Hello, World!");
return (0);
}
Compilation:
$ gcc *.c -o
Output:
$
Hello, World!
$
Upon Working successfully the _printf
returns the number of characters printed
(excluding the terminating null byte used to end output to strings). If an error is encountered,
the function returns -1
.
The conversion specifier (introduced by the character %
) is a character that
specifies the type of conversion to be applied. The _printf
function
supports the following conversion specifiers:
The int
argument is converted to signed decimal notation.
Example:
_printf("%d\n", 12);
_printf("%i\n", -7896);
Output:
12
-7896
The unsigned int
argument is converted to signed decimal notation.
Example main.c
:
_printf("%b\n", 7);
Output:
111
The unsigned int
argument is converted to unsigned octal (o
), unsigned
decimal (u
), or unsigned hexadecimal (x
and X
). The letters abcdef
are
used for x
conversions and the letters ABCDEF
are used for X
conversions.
Example :
_printf("%o\n", 77);
Output:
115
The argument is converted to an unsigned char
.
Example
_printf("%c\n", 'A');
Output:
A
The char *
argument is a pointer to a character array or
pointer to a string is printed starting from first element to the end \0
excluding null byte
Example
_printf("%s\n", "This is a printf function created from scratch");
Output:
This is a printf function created from scratch
Identical to the S
conversion specifier, except any non-printable characters
in the array (ie. characters with an ASCII value < 32 or >= 127) are printed
as \x
followed by the ASCII code value in hexadecimal
Reverse the char *
argument
Example:
int main(void)
_printf("r\n", "Ahmed");
Output:
demhA
Convert the char *
to Rot13
Example:
_printf("%R\n", "Hello, World");
Output:
Uryyb, Jbeyq
The address of the argument is written. The address is written in hexadecimal
with a leading 0x
.
Example:
char *str = "Hello, World";
_printf("%p\n", (void *)str);
Output:
0x561a6d7bab5d
A %
is written. No argument is converted. The complete conversion
specification is %%
.
Example:
_printf("%%\n");
Output:
%
This function also Handle :
- The flag characters for non-custom conversion specifiers (
+
SPACE
#
) - The length modifiers for non-custom conversion(
d, i, u, o, x, X
) specifiers(l
h
)
- Wael Ben Hassen <benhassenwael>
- Ahmed Belhaj <Theemiss>