Skip to content

jdecorte-be/ft-printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft-printf ft-printf

A custom implementation of the C standard library's printf function. This project is part of the 42 school curriculum, handling various format specifiers.

42 School Project Focus Libc Re-implementation Type Static Library Standard C99

Platform Linux | macOS ft-printf license ft-printf stars ft-printf issues ft-printf repo size ft-printf top language

Ft_printfSome examplesOutput:

A custom implementation of the C standard library's printf function. This project is part of the 42 school curriculum and recodes the original printf behavior, providing a lightweight and portable way to format and print data.

Features

This implementation of ft_printf supports the following format specifiers, flags, and features:

Conversion Specifiers

Specifier Output
%c A single character.
%s A string of characters.
%p The memory address of a pointer, in hexadecimal format.
%d A signed decimal integer.
%i A signed decimal integer.
%u An unsigned decimal integer.
%x An unsigned hexadecimal integer (lowercase).
%X An unsigned hexadecimal integer (uppercase).
%% A literal percent sign (%).

Flags and Modifiers

  • -: Left-justify the output within the field width.
  • 0: Zero-pad the output instead of using spaces.
  • .: Specifies precision for strings and integers.
  • #: Alternate form; prepends 0x or 0X for hexadecimal conversions.
  • Width: Specifies a minimum field width for the output.
  • *: Use the next argument as the field width.

Getting Started

Prerequisites

  • A C compiler (e.g., gcc or clang)
  • make build automation tool

Building the Library

  1. Clone the repository:

    git clone https://github.com/jdecorte-be/ft-printf.git
    cd ft-printf
  2. Compile the source files: Run make to compile the project and create the static library libftprintf.a.

    make

Usage

To use ft_printf in your C project, include the header and link against the compiled library.

  1. Include the header file in your source code: #include "ft_printf.h".
  2. Compile your project, linking the libftprintf.a library.

Example

Here is a simple main.c demonstrating the function's usage:

#include "includes/ft_printf.h"

int main(void)
{
    char *str = "world";
    int   num = 42;
    int   char_count;

    ft_printf("--- Testing ft_printf ---\n");
    ft_printf("Hello, %s!\n", str);
    ft_printf("The answer is %d.\n", num);
    ft_printf("Hexadecimal for %d is %#x.\n", num, num);
    ft_printf("Pointer address: %p\n", &num);

    char_count = ft_printf("This line has %d characters.\n", 31);
    ft_printf("The previous line printed %d characters.\n", char_count);

    return (0);
}

Compiling and Running

First, ensure libftprintf.a has been created by running make. Then, compile your main.c with the library:

# Compile the main program and link the library
gcc main.c -L. -lftprintf -Iincludes -o example

# Run the executable
./example

Expected Output

--- Testing ft_printf ---
Hello, world!
The answer is 42.
Hexadecimal for 42 is 0x2a.
Pointer address: 0x7ff7bfeff22c
This line has 31 characters.
The previous line printed 31 characters.

Note: The pointer address will vary on your system.

License

This project is licensed under the terms specified in the LICENSE file.

About

A custom implementation of the C standard library's printf function. This project is part of the 42 school curriculum, handling various format specifiers.

Topics

Resources

License

Stars

Watchers

Forks

Contributors