GitHunt
RI

ricky9leet/1337_ft_printf

Development repository for the 42cursus' ft_printf project

๐Ÿš€ 1337 Project: ft_printf

Development repository for the 42cursus' ft_printf project
For more about 1337 Coding School and its projects, feel free to connect with me on LinkedIn.


๐Ÿ† Project Score

โœ… I achieved a perfect score of 100/100 on this project! ๐ŸŽฏ


๐Ÿ—ฃ๏ธ What is printf?

The printf function (short for print formatted) constructs and displays strings by integrating multiple values based on a specified format.

printf("The value is %d\n", counter);

A format specifier is a special sequence beginning with %, defining how a value should be formatted:

Specifier Description
%d Signed integer (decimal)
%i Signed integer (interprets octal/hex in scanf)
%s String
%c Character
%p Memory address (hexadecimal)
%u Unsigned decimal integer
%x Hexadecimal (lowercase)
%X Hexadecimal (uppercase)
%% Prints a literal %

๐Ÿ” Difference Between %i and %d

In scanf:

  • %d treats 012 as decimal 12
  • %i interprets 012 as octal 10

โš™๏ธ Variadic Functions

A variadic function allows an undefined number of arguments.

๐Ÿ“Œ va_list Structure

typedef struct {
    unsigned int gp_offset;
    unsigned int fp_offset;
    void *overflow_arg_area;
    void *reg_save_area;
} va_list[1];
  • reg_save_area โ†’ Pointer to the start of saved registers
  • overflow_arg_area โ†’ Pointer to stack arguments
  • gp_offset โ†’ Offset to next general-purpose register (default: 48)
  • fp_offset โ†’ Offset to next floating-point register (default: 304)

๐Ÿ”น Key Points:

  • va_list is sometimes just char*
  • va_arg(ap, TYPE) reads memory as TYPE
  • There's no way to determine the length of a va_list

๐Ÿ”ง Key Macros for va_list

Macro Description
va_start(ap, last) Initializes ap before use
va_arg(ap, type) Retrieves the next argument in the list
va_end(ap) Cleans up va_list
va_copy(dest, src) Copies va_list

๐Ÿ—๏ธ Macros in C

A macro is a preprocessor directive (#define) that replaces code segments with predefined values.

๐Ÿ”น Example

#define PI 3.14

๐Ÿ” Types of Macros

Type Description
Object-like Replaces a value or code segment
Function-like Acts like a function with arguments
Chain-like Uses macros inside other macros

๐Ÿ› ๏ธ How to Use

This project includes a Makefile to easily compile the library.

๐Ÿ”น Compilation

Run the following command to compile the library:

make

This will generate the libftprintf.a archive file.

๐Ÿ”น Usage in Your Code

To use ft_printf in your project, include the header and link the library:

#include "ft_printf.h"

Compile your program with:

gcc main.c libftprintf.a -o my_program

๐Ÿ”น Cleaning

To remove object files and binaries:

make clean   # Removes object files
make fclean  # Removes object files and library
make re      # Cleans and recompiles everything

๐Ÿ“š Documentation & References

๐Ÿ›  Technical Articles & Papers

Happy coding! ๐Ÿš€

ricky9leet/1337_ft_printf | GitHunt