Skip to content
Hamza Hdili edited this page Mar 26, 2023 · 1 revision

Welcome to the printf wiki!

Simple Explanation :

This is a C program that implements a basic version of the printf() function, which can print formatted output to the console.

The program defines several functions, including print_string(), print_number(), and print_char(), which are used to handle different types of format specifiers.

The _printf() function takes in a format string and a variable number of arguments, using the va_list and va_start macros to iterate through the arguments.

The program uses a struct called match to match the format specifier with the corresponding function that will print the desired output. If no matching format specifier is found, it prints the character as it is.

For example, the format specifier "%s" is matched with the function print_string(), which takes in a string argument and prints it to the console. Similarly, the format specifier "%d" is matched with the function print_number(), which takes in an integer argument and prints it to the console.

The main.c file simply tests the _printf() function with different input strings and format specifiers.

HOW DOES THE LOOP WORK:

The main loop in the _printf() function iterates over each character in the format string, starting from the first character at index 0. The loop continues until the end of the format string is reached, which is indicated by the null character '\0'.

while (format[i] != '\0')

Inside the loop, the program checks if the current character is a format specifier, which is indicated by the '%' character. If it is not a format specifier, the program simply prints the character using the _putchar() function and moves to the next character in the string.

if (format[i] != '%') {
    _putchar(format[i]);
    len++;
    i++;
    continue;
}

If the current character is a '%', the program looks at the next character to see what type of format specifier it is. To do this, the program uses a nested loop that iterates over each item in the magic array, which contains a list of format specifiers and their corresponding functions.

j = 0;
while (j < MATCH_COUNT) {
    if (magic[j].formater[0] == format[i + 1]) {
        len += magic[j].func(args);
        i += 2;
        break;
    }
    j++;
}

If a matching format specifier is found, the corresponding function is called with the arguments that were passed to the _printf() function. The function returns the number of characters that were printed, which is added to the total length of the output. The loop then moves to the next character in the format string.

If no matching format specifier is found, the program simply prints the '%' character and moves on to the next character in the format string.

if (j == MATCH_COUNT) {
    _putchar('%');
    len++;
    i++;
}

The loop continues until the end of the format string is reached, at which point the total length of the output is returned.

goto:

The goto Beginning statement in the code refers to a label called "Beginning" that appears earlier in the function code.

The label "Beginning" is used as a marker to jump back to the beginning of the loop and start processing the format string again from the next character.

Begining:
    while (format[i] != '\0') {
        // ...
        if (magic[j].formater[0] == format[i] && magic[j].formater[1] == format[i + 1]) {
            len += magic[j].func(args);
            i = i + 2;
            goto Begining;
        }
        // ...
    }

In the context of the code, the goto Beginning statement is used to handle cases where the current character in the format string is a format specifier that matches one of the items in the magic array.

If a matching format specifier is found, the corresponding function is called, and its output is added to the total length of the output. The loop then jumps back to the beginning of the format string to continue processing the rest of the format string.

This allows the program to handle cases where the format string contains multiple format specifiers that need to be processed. By jumping back to the beginning of the loop after each format specifier is processed, the program can handle each format specifier one by one until the end of the format string is reached.

Clone this wiki locally