Skip to content

davidgonzalezfx/printf

Repository files navigation

_printf project

Description

In _printf project we code from zero our own custom printf function. Native printf function allows you to print with certain formats. We handle most basic format so you can print chars, strings, positive and negative numbers, hex, octa and binary numbers among other formats. You can see man 3 of printf to understand how printf works.

Usage

Compile

gcc -Wall -Werror -Wextra -pedantic *.c -o executable

Prototype

int _printf(const char *format, ...)

Return

If everything is successful, the function returns the num of chars printed.

Format conversion

Format Description
c Print char values
s Print strings
d, i Print numbers [positive and negative]
b Print nums converted to binary
u Print nums as unsigned int
o Print nums converted to octal
x Print nums converted to hexa [uppercase]
X Print nums converted to hexa [uppercase]
%% Print % character
p Print memory adress [pointers]
r Print string in reverse
S Print string and non-printable chars [\x + ASCII value in hexa]
R Print in rot13

Examples

  1. Print char values
  • Input _printf("%c", 'x')
  • Output: x
  • Input _printf("%c", 97)
  • Output: a
  1. Print strings
  • Input _printf("Hello%s\n", " Holberton!")
  • Output: Hello Holberton!
  1. Print numbers
  • Input _printf("%d", 1024)
  • Output: 1024
  • Input _printf("%i", -2048)
  • Output: -2048
  1. Print converted to binary
  • Input _printf("%b\n", 15)
  • Output: 1111
  1. Print as unsigned int
  • Input _printf("%u\n", 15)
  • Output: 15
  • Input _printf("%u\n", -15)
  • Output: 4294967281
  1. Print converted to octal
  • Input _printf("%o\n", 15)
  • Output: 17
  1. Print converted to hexa (lower and uppercase)
  • Input _printf("%x\n", 30)
  • Output: 1e
  • Input _printf("%X\n", 30)
  • Output: 1E
  1. Print %
  • Input _printf("%%\n")
  • Output: %
  • Input _printf("%%-%%\n")
  • Output: %-%
  1. Print memory adress [pointers]
  • Input _printf("%p\n", ptr)
  • Output: 0x7ffe637541f0
  1. Print string in reverse
  • Input _printf("Holberton - %r\n", "Holberton")
  • Output: Holberton - notrebloH
  1. Print string and non-printable chars
  • Input _printf("%S\n", "Holberton\nSchool")
  • Output: Holberton\x0ASchool
  1. Print in rot13
  • Input _printf("%R\n", "Holberton")
  • Output: Ubyoregba

Project files

  • Alert: Almost all functions, modify the buffer and returns it. (char *)
File Description
_itoa.c Tools functions:
rev_string: reverse a string passed as argument
string_toupper: convert to uppercase string passed as argument
_calloc: allocate memory and fills it with zeros
_itoa: convert num to base and return as string (unsigned int)
adress: convert num to base and return as string (long int)
_printf.c Core of the project:
_printf:
1. Start the variadic list
2. Retrieves dictonary and allocate buffer
3. Iterates string argument and check for %, if % found then check if the next char match with valid format, in that case call the respective function of these format. If after % there is other char of non-valid format saves in buffer %. If % and the next char is null, returns -1. If % not found, saves in buffer each character
4. When finish loop for string argument, prints all in buffer [print_buff]
5. Free all memory allocated
aux_funs.c Firtst group advanced functions:
print_b: add digits in base 2 to buffer
print_u: add unsigned integers to buffer
print_o: add digits in octal base to buffer
print_x: add digits in hexadecimal base to buffer
print_X: add digits in hexadecimal uppercase base to buffer
buff_funs.c Print all chars saved in buffer:
print_buff: print buffer with null at the end and free buffer memory
holberton.h Header file:
struct funs - filter: Used for dictonary
Include all prototypes
man_3_printf Man page of _printf function
print_funs.c Basic functions:
hand: Dictonary. Creates array of filter's for all valid formats
print_c: add char to buffer
print_str: add string to buffer
print_d: print digits base 10
pr: add percentage to buffer
printf_advance.c Second group advanced functions:
print_p: add pointers - adress to buffer
print_r: add string in reverse to buffer
print_rot13 add string in rot13 to buffer
print_S: add string and non-printable chars [\x + ASCII code]

Authors

David Gonzalez - @davidgonzalezfx
Christian Bedoya - @chrisdav1022
Santiago Mendieta - @stostat