Skip to content

ricky9leet/1337_ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 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

📌 Useful Links

📚 Documentation & References

🛠 Technical Articles & Papers

Happy coding! 🚀

About

Development repository for the 42cursus' ft_printf project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published