Skip to content

Haimasker/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ft_printf

This project was made in accordance with the project of School 21 (Ecole 42).

GitHub code size in bytes Number of lines of code Code language count GitHub top language


Preamble

The purpose of this project is to recode printf function in c.

You can see the subject here: ft_printf.

Main requirements, rules and code style: Norm.

The libft subdirectory is another project for School 21: Libft.


Description

  • The printf() function produces output according to a format as described below.
    The format string is a character string, beginning and ending in its initial shift state, if any.
    The format string is composed of zero or more directives: ordinary characters (not %), which
    are copied unchanged to the output stream; and conversion specifications, each of which results
    in fetching zero or more subsequent arguments. Each conversion specification is introduced by
    the character %, and ends with a conversion specifier. In between there may be (in this order)
    zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.

  • The overall syntax of a conversion specification is:
    %[flags][width][.precision][length modifier][conversion]

Mandatory part

ft_printf() must process following type specifiers:

  1. %c - single character

  2. %s - string of characters

  3. %p - pointer to void (in hexadecimal)

  4. %d - decimal number

  5. %i - integer number

  6. %u - unsigned decimal number

  7. %x - number in hexadecimal (lowercase)

  8. %X - number in hexadecimal (uppercase)

  9. %% - percent character

Bonus part

ft_printf() must manage following format specifiers and minimum field width in any combination:

  1. - - left-justify within the given field width, right justification is the default

  2. 0 - left-pads the number with zeroes instead of spaces, where padding is specified

  3. . - precision (is used with numeric values)

  4. # - adding prefix 0x or 0X for %x or %X type specifiers respectively

  5. + - adding sign (+ or -) in dependency of output value. By default shows only - sign
    for negative values

  6. (space) - adding space before positive output values. By default shows only - sign
    for negative values. Ignored if both + and flags are used

  • Also you can use width specifier (number) to define the field width.

  • Width and precision specifiers can be presented due to * character.
    In this case * should be printed in formatting string for ft_printf at required position instead of
    numeric value. At the same time value for * is passed as argument.

  • For futher information about printf() read the manual or wiki.


Examples

  • _ is showing spaces in output.
Command Output
ft_printf("%3d", 1) __1
ft_printf("%3d", 123456789) 123456789
ft_printf("%3d", -1) _-1
ft_printf("%-3d", 1) 1__
ft_printf("%-3d", 123456789) 123456789
ft_printf("%-3d", -1) -1_
ft_printf("%03d", 1) 001
ft_printf("%03d", 123456789) 123456789
ft_printf("%03d", -1) -01
ft_printf("%+5d", 10) __+10
ft_printf("%-+5d", 10) +10__
ft_printf("%s", "Hello") Hello
ft_printf("%10s", "Hello") _____Hello
ft_printf("%-10s", "Hello") Hello_____

Installation and usage

Makefile compiles given functions into C static library file.

Compiler: gcc

Flags: -Wall -Werror -Wextra


  • Go to the project folder:
$ cd 'path_to_ft_printf'
  • Then typo one of these command:
Command Description
make compiling mandatory and bonus part
make bonus compiling mandatory and bonus part
make clean clearing all .o files
make fclean clearing all .o files and library
  • As you can see make and make bonus have the same behavior.

  • To use compiled project in your code just include library header:

#include "ft_printf.h"
  • While compiling your code add these flags:
-lft -L 'path_to_libftprintf.a' -I 'path_to_ft_printf.h'

Testing