GithubHelp home page GithubHelp logo

mcombeau / ft_printf Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 1.0 2.2 MB

ft_printf is a 42 school project where we must replicate the behavior of C library printf function. The project teaches us about variadic arguments as well as data structures.

Makefile 2.66% C 97.34%
ft-printf42 string-manipulation write 42school c 42 42born2code 42cursus 42paris 42projects

ft_printf's Introduction

ft_printf

ft_printf 42 project badge

For the ft_printf project of the 42 school cursus, we must recreate the famous C library printf function. This project teaches us about variadic arguments as well as structures if we plan to implement printf's extra flags.

  • Supported conversions: %, c, s, p, i, d, u, x, X
  • Supported flags: # + (space)
  • Supported options: - 0 . * width

Status

Finished: 2022-02-07. Grade: 125/100.

Usage

make or make bonus to compile.

Basic Usage

For example, let's create a main.c file.

// Include the header
#include "ft_printf.h"

int main(void)
{
      // Call the function
      ft_printf("Testing ft_printf!");
      return (0);
}

Compile the main.c file with the ft_printf library and run the program:

gcc main.c libftprintf.a && ./a.out

Output should be:

Testing ft_printf!

Advanced Usage: Format Specifiers

This ft_printf function supports several format specifiers, described below. These are optional, can be used in any combination, and are implemented as: %[flags][width][.precision]specifier

The table below lists supported format specifiers:

Flags

Flag Description
- Left justify the result within the field. By default it is right justified.
+ Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a sign.
(space) If there is no sign, a space is attached to the beginning of the result.
# Used with x or X specifiers the value is preceded with 0x or 0X respectively for values different than zero.
0 Leading zeros are used to pad the numbers instead of space.

Width

Value Description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Precision

Value Description
.(number) For integer specifiers (d, i, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For s − this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
.(*) The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Specifiers

Format Specifier Description
% % followed by another % character writes % to the screen.
c writes a single character.
s writes a character string.
p writes an implementation-defined character sequence defining a pointer address.
d or i writes a signed integer to decimal representation.
u writes an unsigned integer to decimal representation.
x or X writes an unsigned integer to hexadecimal representation.

For example, let's ask to print the integer 42 with a width of 20, and then to print it again but with a width of 8, the 0 flag and the + flag:

#include "ft_printf"

int main(void)
{
    ft_printf("Number [%20i]\n", 42);
    ft_printf("Number [%+0*i]\n", 8, 42);
    return (0);
}

Outputs:

Number [                  42]
Number [+0000042]

Made by mcombeau: [email protected] | LinkedIn: mcombeau | Website: codequoi.com

ft_printf's People

Contributors

mcombeau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  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.