In this project, we built our own printf function based on the original one.
_printf - Produces an output according to a format.
#include "main.h"
int _printf(const char *format, ...);The function _printf is our own version of the original printf function.
It has the same behavior and it produces an output according to a format.
You pass a string as the first argument, and you can specify an unlimited number of formats specifiers (more exemple below).
For each format specifier you specify, you have to put an argument who's corresponding to the type of the format specifier.
For example, if you put 3 formats specifiers (one int, one char and one string) you have to put 3 arguments after the formatted string of the corresponding types.
This function is not handling all the functionnality of the original one.
Our function is handling :
- %s: string
- %c: character
- %d: integer value as signed decimal integer
- %i: integer value with decimal, hexadecimal or octal type
- %b: binary
- %u: unsigned integer
- %o: octal format (base 8)
- %X, %x: hexadecimal format (X for uppercase and x for lowercase characters)
- %r: reversed string
- %R: rot13'ed string
To create our _printf function, we mainly used structures, function pointers, recursive functions, variadic functions, loops and conditions.
- write (man 2 write)
- malloc (man 3 malloc)
- free (man 3 free)
- va_start (man 3 va_start)
- va_end (man 3 va_end)
- va_copy (man 3 va_copy)
- va_arg (man 3 va_arg)
_printf("Hello World\n");Produces the output : Hello World
_printf("Hello %s, you have %d years\n", "Bryan", 27);Produces the output : Hello Bryan, you have 27 years
_printf("Your initials are %c%c\n", 'B', 'F');Produces the output : Your initials are BF
_printf("Octal: %i, Decimal: %i, Hexadecimal: %i\n", 010, 8, 0x08);Produces the output : Octal: 8, Decimal: 8, Hexadecimal: 8
_printf("This morning the temperature was %u °C\n", 3);Produces the output : This morning the temperature was 3 °C
_printf("%x is the conversion hexadecimal of %d\n", 1550,1550);Produces the output : 60e is the conversion hexadecimal of 1550
_printf("%r is the inverted display of %s\n", "Hello","Hello");Produces the output : olleH is the inverted display of Hello
_printf("%R is the display of the sentence %s encoded in Rot13\n", "Hello World","Hello World");Produces the output : Uryyb Jbeyq is the display of the sentence Hello World encoded in Rot13
Joel DUMORTIER : GitHub
Philippe WILLOT: GitHub