Team Members:
- Justine Ugwu
- Tiny Bila
This project is part of the ALX-Africa SE program.
In this project, we are implementing our own version of the printf
function in C. The printf
function is a powerful tool for formatted output and is widely used in C programming. By creating our own printf
function, we will gain a deeper understanding of string formatting and manipulating output in C.
The goal of the project is to replicate some of the basic functionality of the standard printf
function, including handling format specifiers such as %c
, %s
, %d
, %x
, and more. We will implement these features while considering different data types, formatting options, and edge cases.
- write (man 2 write)
- malloc (man 3 malloc)
- free (man 3 free)
- va_start (man 3 va_start)
- va_end (man 3 va_end)
- va_copy (man 3 va_copy)
- va_arg (man 3 va_arg)
To get started with the project, follow these steps:
- Clone the repository:
git clone https://github.com/ugwujustine/printf.git
- Compile the code:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c
- man page:
man ./man_printf
## Usage
To use our custom `printf` function, include the `main.h` header file in your C program:
```c
#include "main.h"
int main(void) {
my_printf("Hello, %s! Today is %dth of %s.\n", "Alx", 23, "June");
return 0;
}
Our printf
function supports the following format specifiers:
%c
: Print a single character.%s
: Print a string.%d
: Print a decimal integer.%x
: Print an integer in hexadecimal format.- ...
Refer to the code implementation for additional details on the supported format specifiers and their usage.
Here are a few examples demonstrating the usage of our printf
function:
my_printf("Hello, %s!\n", "World");
// Output: Hello, World!
my_printf("The value of x is %d and in hexadecimal it is %x.\n", 42, 42);
// Output: The value of x is 42 and in hexadecimal it is 2a.
my_printf("The character is %c.\n", 'A');
// Output: The character is A.
Name | Information | Relevant Files |
---|---|---|
man_printf |
Man page of the _myprintf() function. | None |
main.h |
Header file with the data type struct, standard libraries and custom prototypes. | *.c compilation |
_putchar.c |
Custom putchar function. | None |
Write a function that produces output according to a format. Handle the following conversion specifiers:
- c
- s
- %
Handle the following conversion specifiers:
- d
- i
Handle the following conversion specifiers:
- b
Handle the following conversion specifiers:
- u
- x
- o
- x
- X
Use a local buffer of 1024 chars in order to call write as little as possible.
- S : prints the string.
- 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).
Handle the following conversion specifier: p
Handle the following flag characters for non-custom conversion specifiers:
- ´+´
- space
- ´#´
Handle the following length modifiers for non-custom conversion specifiers:
- l
- h Conversion specifiers to handle: d, i, u, o, x, X
Handle the field width for non-custom conversion specifiers.
Handle the precision for non-custom conversion specifiers.
Handle the 0 flag character for non-custom conversion specifiers.
Handle the - flag character for non-custom conversion specifiers.
Handle the following custom conversion specifier:
- r : prints the reversed string
Handle the following custom conversion specifier:
- R: prints the rot13'ed string
All the above options work well together.
This project is licensed under the MIT License.