The printf function is used to print formatted text to the standard output. It is included in the stdio.h file of the standard library.
The format string contains zero or more directives, which can be either literal characters or encoded conversion specifications that describe how to format arguemt(s) in the output.
The general format of printf() function is as follows:
int printf(const char *format, ...);
In the above, format is a string that contains text to be printed to the standard output. It can optionally contain embedded format tags that are replaced by values specified in subsequent arguments and formated as requested. The ... is an ellipsis which indicates that the function can take a variable number of arguments.
When printf is run alone, it returns an integer value which represents the number of characters transmitted to the standard output stream. If output/encoding error occurs, printf returns a negative value.
My partner and I created a function that imitates the printf function.
This contains all the function prototypes used in the manual implementation of the printf function. It also contains a typedef declaration of struct format. This structure has two members: 'my_spec', which is a character that represents the format specifier, and 'fun', which is a pointer to the function that should be called when the format specifier is encountered in the _printf() function.
It also contains some header files from the C standard library - notably, stdarg.h - which are used in this implementation.
The header file is appropriately guarded to avoid multiple definitions of the same header file.
#ifndef MAIN_H #define MAIN_H
#include <stdarg.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
/**
- struct format - the format specifier character
- @my_spec: pointer to the function that should be called
- @func: declares a pointer function */
typedef struct format
{
char my_spec;
int (*func)(va_list);
} fmt;
int handle_format(const char *format, int *i, va_list my_args);
int _printf(const char *format, ...);
int handle_char(va_list my_args);
int handle_string(va_list my_args);
int handle_percent(va_list my_args);
int handle_d(va_list my_args);
int handle_i(va_list my_args);
int handle_num(unsigned int n);
int handle_u(va_list my_args);
int handle_o(va_list my_args);
int handle_x(va_list my_args);
int handle_X(va_list my_args);
int print_bin(unsigned int num);
int handle_bin(va_list my_args);
int handle_p(va_list my_args);
int convert_hex(unsigned long int n, char *hex);
void reverse_string(char *str, int length);
int handle_r(va_list my_args);
char *_rot13(char *str);
int handle_R(va_list my_args);
int _putchar(char c);
#endif
Custom printf that produces output according to a format.
Function that handles 'd' integer specifiers and prints the corresponding integer.
Function that handles 'i' integer specifiers and prints the corresponding integer.
Handles the format specifier in the given format string.
Writes the character c to stdout.
Function that handles 'b' specifier and prints the binary representation of an unsigned integer.
Auxiliary function that prints the binary representation of an unsigned integer.
Converts an unsigned long int to a hex string.
Reverses a string.
Handles the %p conversion specifier and prints the memory address.
Handles the %r conversion specifier and prints a string in reverse.
Encodes a string using ROT13 encryption.
Handles the %R conversion specifier and prints a string encoded in ROT13.
Function to print an integer digit by digit.
Function to handle 'u' specifier and print an unsigned integer.
Function to handle 'o' specifier and print an octal representation of an unsigned integer.
Function to handle 'x' specifier and print a hexadecimal representation (lowercase) of an unsigned integer.
Function to handle 'X' specifier and print a hexadecimal representation (uppercase) of an unsigned integer.
Function that handles 'c' specifier and prints a character.
Function that handles 's' specifier and prints a string.
Handles the '%%' specifier and prints a '%' character.