Coder Social home page Coder Social logo

devawizard / printf_by_anwu-yan Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 50 KB

Developed a custom printf function in C, mastering variadic functions, format specifiers, and diverse data type handling for tailored output control.

License: MIT License

Makefile 3.55% C 96.45%
42commoncore 42cursus 42printf 42school prinft 42urduliz

printf_by_anwu-yan's Introduction

cover-ft_printf

🖨️ My own printf function 🖨️

Project introduction

Welcome to the libftprintf project, a custom implementation of the printf function from the C standard library. As part of the 42 School curriculum, this project challenges you to recreate one of the most complex functions in the C library, focusing on specific conversions to deepen your understanding of variadic functions in C. The goal of ft_printf is not just to mimic the behavior of the original printf function but to explore the intricacies behind its implementation. Through this project, you will enhance your programming skills and gain valuable insights into the workings of variadic functions, all while adhering to the rigorous standards of the 42 School.

⭐ Table of Contents ⭐

Project Name

  • Program Name: libftprintf.a

Files to Submit

  • Required Files: Makefile, *.h, /.h, *.c, /.c
  • Library File: libftprintf.a to be created at the root of your repository.

Makefile Rules

  • Rules: NAME, all, clean, fclean, re

Authorized Functions

  • libc Functions: malloc, free, write
  • Variadic Functions: va_start, va_arg, va_copy, va_end
  • External Libraries: Yes, using libft is allowed.

Using libft

  • This project permits the use of the libft library to assist with various functionalities.

Description

  • The goal is to write a library that contains the ft_printf function, mimicking the original printf.
  • Prototype: int ft_printf(const char *, ...);
  • Key Requirements:
    • Do not implement the original printf buffer management.
    • Implement the following conversions: cspdiuxX%
    • Your function will be compared against the original printf.
    • Use the ar command to create your library. The use of the libtool command is forbidden.

Conversion Specifications

  • %c Prints a single character.
  • %s Prints a string (as defined by C standards).
  • %p Prints a void * pointer in hexadecimal format.
  • %d Prints a decimal (base 10) number.
  • %i Prints an integer in base 10.
  • %u Prints an unsigned decimal (base 10) number.
  • %x Prints a hexadecimal (base 16) number in lowercase.
  • %X Prints a hexadecimal (base 16) number in uppercase.
  • %% To print the percent symbol.

Requirements

  • Your libftprintf.a library must be created at the root of your repository.
  • You must use the ar command to create your library; using libtool is forbidden.

Implementation overview

Understanding printf and Its Replication

Overview of printf Function

The printf function is a standard output function in C, used to print formatted data to the stdout. It's defined in <stdio.h> and has the following prototype:

int printf(const char *format, ...);

The format string includes text to be printed and format specifiers, which are placeholders for variables that will replace them. The ellipsis (...) indicates that printf can accept an arbitrary number of arguments after the format string, matching the format specifiers in both type and order.

How printf Works

  • Parsing the Format String: printf reads the format string character by character, identifying format specifiers after each percent sign (%). These specifiers dictate how to interpret and format the subsequent arguments.

  • Fetching Arguments: Based on the format specifier, printf fetches the next argument from the stack, ensuring the types of these arguments match the specified format.

  • Formatting and Printing: Arguments are formatted according to their specifiers and added to the output buffer, with any remaining text in the format string also printed.

  • Handling Flags and Width: Format specifiers can include flags, width, precision, and length modifiers, allowing for detailed formatting control, such as padding numbers with zeros.

  • Return Value: printf returns the total number of characters written, excluding the null byte.

Replicating printf

To replicate printf, the following steps are necessary:

  • Parse Format Strings: Implement a function to parse format strings and identify format specifiers, flags, width, precision, and length modifiers.

  • Fetch and Process Arguments: Use C standard library functions like va_start, va_arg, and va_end to fetch and process arguments of various types.

  • Conversion to String Representations: Convert arguments into their string representations according to the format specifiers.

  • Output Management: Manage a buffer for assembling the output string before printing it to stdout.

  • Error Handling: Ensure the function returns the number of characters printed or a negative value if an error occurs.


printf_by_anwu-yan's People

Contributors

devawizard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.