What this function does is to go through the format constant character by character, and according to the character it finds, it evaluates and performs a function.
To be more specific, the format string includes the text to be printed literally and as it proceeds, it will check if the current character is a '%', when it finds this format indicator, in case the next character is a format specifier, it replaces it with the text obtained from the added parameters, in case it is another '%', it simply prints that '%', if it is neither a format type nor a '%', it simply prints the % followed by the character after that '%'.
Example:Input:
int mian()
{
_printf("Character:[%c]|n", 'H');
return(0);
}Output:
Character:[H]
The symbol '%' denotes the beginning of the formatting mark.
If several indicators are pickled in the format constant string, the values are included in the same order in which they appear.As shown in the following example:
Input
int main()
{
_printf("Color %s, number %d", "red", 1234567);
return (0);
}Output:
Color red, number 1234567
_printf("% [ format ]", type);
Format type field
| format types | description |
|---|---|
| %c | Prints the corresponding ASCII character |
| %s | Character string (ending in '0') |
| %d, %i | Signed decimal conversion of an integer |
| %b | the unsigned int argument is converted to binary |
| %u | Unsigned decimal conversion of an integer |
| %o | Unsigned octal conversion of an integer |
| %x, %X | Unsigned hexadecimal conversion, x for lowercase, X for uppercase |
| %r | prints the reversed string |
| %R | prints the rot13'ed string |
| %p | Memory address (pointer) |
// clones the repository
$ git clone https://github.com/Sapitorico/holbertonschool-printf
$ cd holbertonschool-printf
// creates a main call function
$ make all
gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format *.c -o printf
./printf
// output examples
Negative:[-762534]
Unsigned:[2147484671]
Unsigned octal:[20000001777]
Unsigned hexadecimal:[800003ff, 800003FF]
Character:[H]
String:[I am a string !]Compilation:
// You might want to look at the gcc flag -Wno-format when testing with your printf and the standard printf. Example of test file that you could use:
$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -Wno-format *.ctesting function
- INPUTS:
#include "main.h"
int main(void)
{
int len;
unsigned int ui;
void *addr;
char *str = "Sapitorico";
len = _printf("Let's try to printf a simple sentence.\n");
ui = (unsigned int)INT_MAX + 1024;
addr = (void *)0x7ffe637541f0;
_printf("Length:[%d, %i]\n", len, len);
_printf("Negative:[%d]\n", -762534);
_printf("Unsigned:[%u]\n", ui);
_printf("Unsigned octal:[%o]\n", ui);
_printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
_printf("Character:[%c]\n", 'H');
_printf("String:[%s]\n", "I am a string !");
_printf("Address:[%p]\n", addr);
len = _printf("Percent:[%%]\n");
_printf("Len:[%d]\n", len);
_printf("Unknown:[%r]\n", str);
_printf("Prints the rot13'ed:[%R]\n", str);
return (0);
}- OUTPUTS:
Let's try to printf a simple sentence.
Length:[39, 39]
Negative:[-762534]
Unsigned:[2147484671]
Unsigned octal:[20000001777]
Unsigned hexadecimal:[800003ff, 800003FF]
Character:[H]
String:[I am a string !]
Address:[0x7ffe637541f0]
Percent:[%]
Len:[12]
Unknown:[ocirotipaS]
Prints the rot13'ed:[Facvgbevpb]
This file contains the main code of the printf function. In this one the function get_formats is invoked to look for the functions of formats, and this same function is the one that is in charge of sending the parameters to these functions to print formats.
Prototype: int _printf(const char *format, ...);
this file has all the maros headers used by the functions, function prototypes and structure.
/**
* struct get_formats - contains the formats
* @f_s: format specifier
* @f: pointer to formatting functions
*
* Description: this structure contains the format indicators in the f_s layer, and the functions corresponding to these formats in the field f
*/
typedef struct get_formats
{
char f_s;
int (*f)(va_list args);
} get_t;get_t functions[ array of formats and functions ]
-
[ c ] Converts an argument of type int to a value of type unsigned char and writes the corresponding ASCII character code to the output stream.
-
[ s ] Writes the characters of the string specified by an argument of type char *, up to, but not including the terminating NUL character ('\0'), to the output stream.
-
[ x, X ] Converts an unsigned argument to unsigned hexadecimal notation, and writes it to the output stream. The default precision is 1, but if more digits are needed, leading zeros are added. Hexadecimal notation uses the digits 0 through 9 and the characters a through f or A through F for x or X conversions, respectively, as hexadecimal digits. Subject to the control flag alternatively, 0x or 0X is prefixed to the output.
-
[ R ] Encrypt a string in ROT13, rotate 13 positions" a letter, moving any letter 13 positions in the alphabet.
-
[ d, i ] Converts an int argument to signed decimal notation and writes it to the output stream.
-
[ b ] Converts an unsigned integer argument to binary
-
[ u ] Converts an unsigned argument to unsigned decimal notation, and writes it to the output stream.
-
[ o ] Converts an unsigned argument to unsigned octal notation and writes it to the output stream.
- [ p ] Converts an argument of type void * to a value of type int, and formats the value as for a hexadecimal conversion (x).
- printf function manual, to view the man page, enter:
man ./_printf.man
This is the first group project, carried out by Holberton students. The goal of this assignment is to recreate the printf function, a basic version of the standard function. It also encourages group and team work with a randomly assigned partner.
Requirements
-
Allowed editors: vi, vim, emacs.
-
You are not allowed to use global variables.
-
No more than 5 functions per file.
-
it is not necessary to upload the test network to your repository.
-
The prototypes of all your functions should be included in your header file called main.h.
-
Note that we will not provide the putchar function for this project.
Authorized functions and macros
-
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)
Authors:
