This repository contains the group project 0x11. C - printf
In this project, we implement a function that behaves in a similar way to the printf function provided in the Standard library.
The directory contains several C source files and one header file.
The function being implemented in this project is the _printf function.
int _printf(const char *format, ...);
format is a constant string that acts as the format template. It indicates the various items that are to be printed and how they will appear when printed. If the % symbol preceeds a character, it indicates that there is an argument that is expected in its place. The function will look through the ordered list of optional arguments and expand it in place of the format specifer and then print it.
The function returns the number of individual characters that have been printed.
my_printf.c
This source file is where the _printf function is implemented.
get_specifier.c
This source file is where the get_specifier function is implemented.
This function takes in a pointer to a character, from the format template, as an argument and determines the appropriate funtion pointer to return, that will handle the given format specifier.
int (*get_specifier(const char *)(va_list args)
It relies on a structure that represents a key-value pair that maps a format specifier to its appropriate function. Depending on the character provided, usually following a % symbol in the format template, it returns a pointer to the function that handles the argument that is to be expanded and printed in its place.
struct funcs
{
const char *type;
int (*func)(va_list);
};
Here, the key is the pointer to a character that indicates the type of format specifier, while the value is pointer to a functinon that takes an argument of type `va_list`.
handlers.c
In this source file, the various functions that handle various format specifiers are defined.
main.h
This header file contains all function prototypes of the functions used across all C source files in this directory.
It also has structures that have been defined for purposes of function implementation.
u_o_x_handlers.c
This source file contains the function oct_print, hex_print and unsigned_int that handle the %o, %x and %u format specifiers respectively.
Each function has the function signature:
int func_name(va_list, char *, int);
where the first formal paramater is a variable of type va_list from which the optional argument to be printed is etracted. The second parameter is a pointer to a character array that represents a buffer of size 1024 bytes, where the characters to be written onto the standard output are held. The third parameter is the buffer index at the time of the function call the _printf function.
The three functions also return the number of characters that have been added to the buffer (characters that will be printed from the optional argument they have handled).
s_handler.c
This source file contains the function s_print that is a custom conversion specifier %S that prints a string. However, non printable characters (0 < ASCII value < 32 or >= 127) are printed in this way: \x, followed by the ASCII code value in hexadecimal (upper case - always 2 characters).
_printf("%S\n", "Best\nSchool");
================================
Output: Best\x0ASchool
The code above shows a use case.