This project is a custom implementation of the printf function. It is divided into several source files that handle different aspects of formatted output, such as buffering, flag processing, number conversion, and string handling.
-
main.h
Contains the header definitions for the project. It defines key structures such asparams(to store flag information, buffer pointers, and other formatting data) andid_f(to associate format specifiers with their corresponding functions). Function prototypes for the main _printf function and its helper routines are declared here. -
_printf.c
Implements the primary_printffunction. It parses the format string, handles flags and conversion specifiers, and writes formatted output to a buffer, which is later printed to standard output. -
buffer_tools.c
Provides functions to manage the output buffer. Functions include:fill_buffer_c: Adds a single character to the buffer.fill_buffer_str: Adds a string to the buffer.print_buffer: Flushes the buffer content to standard output.
-
print_base.c
Contains functions that convert numbers to various bases:_print_binary: Prints an unsigned integer in binary format._print_oct: Prints a number in octal._print_hex: Prints a number in hexadecimal (lowercase)._print_hexa: Prints a number in hexadecimal (uppercase)._print_ptr: Prints the memory address of a pointer in hexadecimal format with a "0x" prefix.
-
print_strings.c
Provides functions for printing characters and strings in various ways:_print_char: Prints a single character._print_str: Prints a string (handlesNULLby printing(null))._print_rev: Prints a string in reverse order._print_rot13: Encodes a string using ROT13 before printing it._print_S: Prints a string while converting non-printable characters (ASCII values below 32 or above 126) into their hexadecimal representation.
-
flags_handling.c
Implements functions to detect and set formatting flags and options from the format string. This includes functions to handle flags (flags_set), width (width_set), precision (precs_set), length modifiers (length_set), and setting the conversion specifier (id_set). -
general_tools2.c
Contains utility functions such as:get_op_func: Returns the appropriate function pointer based on the conversion specifier.extract_value: Extracts numerical values from the format string (for width or precision), supporting both direct numbers and '*' to fetch the value from the argument list.
-
general_tools.c
Provides additional utility functions including:_strlen: Calculates the length of a string._strdup: Duplicates a given string into a new memory area.in_charset: Checks if a character exists within a given character set._atoi: Converts a string to an integer.flags_init: Initializes and validates formatting flags from the format string.
-
output_modifiers.c
Implements modifications to the printed output based on specific flags. For example, adding a plus sign for positive numbers or a space if no plus sign is specified. It also handles special cases for octal and hexadecimal prefixes when the '#' flag is used. -
print_numbers.c
Contains functions for printing numeric values:_print_int: Handles printing of signed integers, including negative numbers._print_uint: Handles printing of unsigned integers.
| Specifier | Functionality |
|---|---|
%c |
Prints a single character. |
%s |
Prints a string. If the string is NULL, prints (null). |
%d |
Prints a signed integer. |
%i |
Prints a signed integer (identical to %d). |
%b |
Prints an unsigned integer in binary format. |
%u |
Prints an unsigned integer in decimal format. |
%r |
Prints a string in reverse order. |
%R |
Prints a string encoded in ROT13. |
%o |
Prints a number in octal format. |
%x |
Prints a number in hexadecimal format (lowercase). |
%X |
Prints a number in hexadecimal format (uppercase). |
%p |
Prints the memory address of a pointer in hexadecimal format with a 0x prefix. |
%S |
Prints a string and converts non-printable characters into hexadecimal codes. |
#include "main.h"
int main(void)
{
int num = 42;
char *str = "Hello, World!";
_printf("String: %s\n", str);
_printf("Character: %c\n", 'A');
_printf("Integer: %d\n", num);
_printf("Binary: %b\n", num);
_printf("Pointer: %p\n", str);
_printf("Reverse: %r\n", str);
_printf("ROT13: %R\n", str);
_printf("Hex lowercase: %x\n", num);
_printf("Hex uppercase: %X\n", num);
_printf("Special string: %S\n", "Test\nNewLine");
return (0);
}| Flag | Description |
|---|---|
+ |
Adds + before positive numbers. |
| (space) | Adds a space before positive numbers if + is absent. |
# |
Adds 0, 0x, or 0X for octal and hexadecimal values. |
0 |
Pads numbers with zeros when width is specified. |
- |
Left-aligns output within a given width. |
The entire modifier flag was handled, but only some functionalities was developed.
_printf("String: %s\n", "Hello");
_printf("Character: %c\n", 'A');
_printf("Integer: %d\n", 42);
_printf("Binary: %b\n", 42);
_printf("Pointer: %p\n", (void *)42);_printf("With plus: %+d\n", 42); // Output: +42
_printf("With space: % d\n", 42); // Output: 42gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -Wno-format *.cOnce compiled with your main.c archive, you can run the executable and use the custom _printf function in your code as shown in the examples.
