A collaboration between Athena Deng and Andrew Lindburg, this was the first team project in the Holberton School curriculum. After 5 weeks of learning the C programming language, the goal of this project was to create a function that was like the C standard library's printf within 5 days.
For this project we had to write a function that produced output according to a format.
- Function prototype:
int _printf(const char *format, ...); - Return: the number of characters printed (excluding the null byte used to end output to strings).
- Write output to stdout, which is the standard output stream.
formatis a character string. The format string is composed of zero or more directives. Seeman 3 printffor more detail.- The following conversion specifiers are handled:
- %c
- %s
- %d
- %i
- We did not have to reproduce the buffer handling of the C library
printffunction. - We did not have to handle the flag characters.
- We did not have to handle field width.
- We did not have to handle precision.
- We did not have to handle the length modifiers.
- We may not have more than 5 functions per file.
- Each function must end with a newline.
- Global variables are not allowed.
- All files use the
Bettystyle and were checked using betty-style.pl and betty-doc.pl. - The executable compiled from all of the
.cfiles prints to standard output the formatted arguments that were passed to the_printffunction. - Authorized functions and macros for this project are:
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)
- managing dynamically allocated memory
- handling unexpected user input like
%%cor% i - correctly formatting a manual page
This is the manual page for our _printf function. To view it as a manual page like the image below, run this command:
$ man ./man_3_printf
- All of the
.cfiles along with amain.cfile are to be compiled withgcc 4.8.4on Ubuntu 14.04 LTS with the flags-WallWerror-Westraand-pedantic. - The files will be compiled this way:
$ gcc -Wall -Werror -Wextra -pedantic *.c
In the main.c file, use the _printf function like so:
$ cat main.c
#include "holberton.h"
/**
* main - main function of program
* Return: always 0
*/
int main(void)
{
int num;
char *string;
num = 12;
string = "Hello, world!"
_printf("%s I am not %i years old!\n", string, num);
return (0);
}
$ gcc -Wall -Werror -Wextra -pedantic *.c -o myprogram
$ ./myprogram
Hello, world! I am not 12 years old!
$
