Skip to content

abfabs/holbertonschool-printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

_printf – Custom printf Function in C

1. Description

We built a simplified version of the C printf function called printf. The goal was understanding how formatted output actually works, instead of just using the standard library version. The function loops through whatever string you give it and, whenever it sees a percent sign, it figures out what to do based on the next character.

A few ideas were connected in C to make it all work, like pointers, structures, and variadic functions. For each type of value (like a character or an integer), there’s a separate function that knows how to print it.

Also, a custom struct was made to match each specifier with the function that handles it. It was stored in an array, so when printf sees something like %d, it uses that array to find the right function and call it. The rest of the time, it just prints the characters as they are. We also handled edge cases like double percentage signs or null strings. Finally, our custom function adds up how many characters were printed and returns that number.

Format Specifiers in _printf

Specifier Description Example Output
%c Single character _printf("%c", 'A') A
%s String ((null) if argument is NULL) _printf("%s", NULL) (null)
%% Literal percent sign _printf("%%") %
%d / %i Signed decimal integer (base 10) _printf("%d", -5) -5

Custom _printf flowchart

Basic usage

_printf("Hello, %s!\n", "from Tirana");
Hello, from Tirana!

Print integers

_printf("Number: %d\n", 1711);
_printf("Negative: %i\n", -2025);
Number: 1711
Negative: -2025

Print a character

_printf("Alpha letter: %c\n", 'A');
Alpha letter: A

Print a percentage symbol

_printf("Progress: 100%% complete.\n");
Progress: 100% complete.

NULL string handling

_printf("This string should be null: %s\n", NULL);
This string should be null: (null)

Invalid specifier handling

_printf("Unknown specifier: %q\n");
Unknown specifier: %q

▪ Function Prototype

int _printf(const char *format, ...);
  • format: A string that may contain regular characters and/or special instructions starting with %, called format specifiers, which direct how values should be printed.
  • ... : Variadic arguments corresponding to each format specifier

▪ Return Value

  • Returns the total number of characters printed (excluding the null byte).

  • Returns -1 if:

    • format is NULL, or
    • the format string ends with an incomplete format specifier (e.g., % and no following character).

2. Key Functionalities

Supported format specifiers:

  • %c – Prints a character
  • %s – Prints a string
  • %d – Prints a signed decimal integer
  • %i – Prints a signed decimal integer
  • %% – Prints a percent sign

3. Compilation

To compile the project, the following command is used in the terminal:

gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o printf_test

4. Requirements

  • GCC compiler
  • Ubuntu 20.04 LTS or compatible environment
  • C standard: C89 (GNU89)
  • Code style: Betty compliant

Included Files:

  • _printf.c: Core logic — parses format string and calls appropriate handlers
  • _putchar.c: Low-level function to print one character to stdout
  • print_functions.c: Contains all specifier handlers: %c, %s, %d, %i
  • main.h: Header file with structs and function prototypes
  • main.c: Test file with various usage examples

5. Examples and Testing

6. License

This project is for educational purposes only and is part of the Holberton School / Holberton Basics curriculum.

7. Authors

Alba Eftimi                      Sokol Gjeka
GitHub: abfabs                GitHub: sokolgj19

July 2025
Tirana, Albania

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages