Skip to content
/ printf Public
forked from AnasBenba/printf

This project is a custom implementation of the printf function in C. It allows users to format and print text to the console in a wide range of formats and styles. The implementation includes support for common format specifiers such as `%s`, `%d`, `%c`, `%x`, and `%X`, as well as flags such as `+`, `#`, space, `l`, and `h`.

Notifications You must be signed in to change notification settings

4MR4N11/printf

 
 

Repository files navigation

Custom printf function

This repository contains a custom implementation of the printf function in C. It was developed as part of the curriculum for the ALX School Full-Stack Software Engineering program.

Functionality

The custom printf function supports the following:

Format Specifiers

  • %c: print a single character
  • %s: print a null-terminated string
  • %d: print a decimal integer
  • %i: print an integer
  • %b: print an unsigned integer in binary format
  • %x: print an unsigned integer in lowercase hexadecimal format
  • %X: print an unsigned integer in uppercase hexadecimal format
  • %o: Print an unsigned integer in octal format.
  • %S: Print a string but the Non printable characters (0 < ASCII value < 32 or >= 127) are printed this way: \x, followed by the ASCII code value in hexadecimal (upper case - always 2 characters)
  • %p: Print a memory address of a pointer variable in hexadecimal format.
  • %%: Print a literal percent sign

Flags

  • +: Forces to precede the result with a plus or minus sign (+ or -)
  • #: Applies the "alternate form" for o, x, and X conversions
  • (space): Leaves a blank space before a positive number

Length Modifiers

  • h: Short (for integer types)
  • l: Long (for integer types)
  • ll: Long long (for integer types)

File Structure

The source code for the custom printf function is organized as follows:

  • _printf.c: contains the implementation for the custom printf function
  • _put_bin.c: contains the implementation for printing unsigned integers in binary format
  • _put_num.c: contains the implementation for printing decimal and integer values
  • _put_X.c: contains the implementation for printing unsigned integers in uppercase hexadecimal format
  • _put_x.c: contains the implementation for printing unsigned integers in lowercase hexadecimal format
  • _putstr.c: contains the implementation for printing null-terminated strings
  • _put_o.c: contains the implementation for printing unsigned integer in octal format.
  • _put_S.c: contains the implementation for printing string of characters with non-printable characters encoded.
  • _put_p.c: contains the implementation for printing pointer address.
  • main.h: contains the function prototypes and necessary header files.

Usage

To use the custom printf function in your own program, simply include the main.h header file and call the function with the desired format string and any necessary arguments.

Example usage:

#include "main.h"

int main(void)
{
    _printf("%s %d %b %#X %o\n", "Hello, world!", 42, 42, 255, 42);
    return (0);
}

This will output:

Hello, world! 42 101010  0XFF 52

Testing

The custom printf function has been tested on a variety of input strings and format specifiers. To run the test suite, simply compile and run a main file along with all the files:

gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format *.c -o test_printf
./test_printf

This will compile and run the test suite, which will output the results of each test.

Authors

About

This project is a custom implementation of the printf function in C. It allows users to format and print text to the console in a wide range of formats and styles. The implementation includes support for common format specifiers such as `%s`, `%d`, `%c`, `%x`, and `%X`, as well as flags such as `+`, `#`, space, `l`, and `h`.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 100.0%