_printf - produce output according to a format
#include "main.h"
int _printf(const char *format, ...);
The _printf function produces output according to a format as described below. The format string is composed of zero or more directives. The function writes the output to stdout, the standard output stream.
The format string is a sequence of characters that starts and ends in its initial shift state, if applicable. This string can include one or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which fetches zero or more subsequent arguments. Each conversion specification begins with the character % and ends with a conversion specifier.
The following conversion specifiers are supported:
- %c: Prints a single character using the internal function _char().
- %s: Prints a string of characters using the internal function _str().
- %%: Prints a literal percentage '%' using the internal function porcentaje.
- %d: Prints a decimal (base 10) number, using the internal function __int()_.
- %i: Prints an integer in base 10, using the internal function __int()_.
int _printf(const char *format, ...);
- Description: Produces output according to a format.
- Parameters: format -> A character string composed of zero or more directives.
- Return Value: The number of characters printed (excluding the null byte).
int _putchar(char c);
- Description: Writes a character to stdout.
- Parameters: c -> The character to be written.
- Return Value: On success, returns the character written. On error, returns -1.
int _char(va_list args);
- Description: Print a character.
- Parameters: args -> List of arguments containing the character to be printed.
- Return Value: The number of characters printed.
int _str(va_list args);
- Description: Prints a string of text.
- Parameters: args -> List of arguments containing the string to be printed.
- Return Value: The number of characters printed.
int porcentaje(va_list args);
- Description: Handles %% prints %.
- Parameters: args -> List of arguments.
- Return Value: The number of characters printed.
int _int(va_list args);
- Description: Prints a postive o negative integer.
- Parameters: args -> List of arguments containing the integer to be printed.
- Return Value: The number of characters printed.
int (*get_op_func(char s))(va_list args);
- Description: Receives a character and returns the associated function.
- Parameters: s -> Character.
- Return Value: The function associated with the character, or NULL if no match is found.
typedef struct especificador
- Description: Defines a structure that associates a format specifier with the corresponding function that handles that format.It is used alongside the get_op_func function to dynamically select the appropriate function according to the specifier found in the format string.
- Parameters:
- op -> The character representing the format specifier.
- f -> A pointer to the function that prints the argument corresponding to the specifier.
- Return Value: Not applicable; this structure is only a data type definition.
The _printf functions returns the number of characters printed (excluding the NULL byte used to end output to strings). If the format argument is NULL or if an invalid specifier is provided, the function returns -1.
#include "main.h"
int main(void)
{
_printf("Character:[%c]\n", 'H');
_printf("String:[%s]\n", "I am a string !");
_printf("Percent:[%%]\n");
_printf("Decimal:[%d]\n", 123);
_printf("Integer:[%i]\n", 456);
return (0);
}
| CASE | OUTPUT |
|---|---|
_printf("Hello, %s!\n", "World"); |
Hello, World! |
_printf("The number is: %d\n", 30); |
The number is: 30 |
_printf("Charater: %c\n", 'A'); |
Character: A |
_printf("Integer: %dn", 123); |
123 |
_printf("Percent sign: %%\n"); |
% |
_printf("Hello, %s! Character: %c, Integer: %d, Percent sign: %%\n", "World", 'A', 123); |
Hello, World! Character: A, Integer: 123, Percent sign: % |
_printf("Result: %d\n", result); |
-1 |
_printf("Negative integer: %d\n", -456); |
-456 |
_printf("Null string: %s\n", NULL); |
NULL |
_printf("String: %s, Character: %c, Integer: %d, Percent: %%\n", "test", 'B', 789); |
String: test, Character: B, Integer: 789, Percent: % |
_printf("Large number: %d\n", 123456789); |
123456789 |
_printf("Positive decimal: %f\n", 234.456); |
234.456000 |
_printf("Negative decimal: %f\n", -234.456); |
-234.456000 |
The _printf function handles errors gracefully. If an invalid format specifier is encountered, the function will return -1 and print an error message to stderr.
The current implementation of _printf does not support the following:
- Flag characters
- Field width
- Precision
- Length modifiers
The _printf function is a limited implementation of the standard printf function from the C standard library. It does not support advanced specifiers such as %f, %x, %u, %p, among others
To test the _printf function, compile your code using the following command:
$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -Wno-format *.c
Run the executable to see the output:
$ ./_printf
==================================================================================
Written by Marrero, Martín & Arévalo, Alejandro
==================================================================================