- Function that prints argument(s) according to format
- Created in language C and only can be used in it;
- Some format controls are used;
- Based in the version written by David MacKenzie
Table of Contents
[TOC]
In this function we define a first parameter where we have the data to print and it will be verified if it has any identifier operator, the other parameters will correspond to the identifier operators found in the first.
int _printf(const char *format, ...);This is the main function which contains all the execution processes connected to the secondary functions, it return a integer value corresponding a total string lenght and use a variadic constant char pointer.
Now, we will show the different use cases of this function:
#include "holberton.h"
int main()
{
_printf("Example0: Printing a simple text\n");
return (0);
}Example0: Printing a simple text with
Only we needs to put a string value into double quotes without special characters as '%' because it used for identify other type formats
#include "holberton.h"
int main()
{
_printf("Example1: Printing a simple text %s", "with another text\n");
return (0);
}Example1: Printing a simple text with another text
In this case, we uses a % with the 's' into the first parameter for indicate where it puts the data of the second parameter that corresponding a string value with format pointer.
#include "holberton.h"
int main()
{
char txt1[] = "with another text", txt2[] = "and other\n";
_printf("Example2: Printing a simple text %s %s", txt1, txt2);
return (0);
}Example2: Printing a simple text with another text and other
#include "holberton.h"
int main()
{
int num1 = 6;
_printf("Example3: Printing a integer 6 as %d\n", num1);
return (0);
}Printing a integer 6 as 6
We uses another identify format for the integer numbers that is %d, the output shows that integer stored in the variable num1 replacing the identify format.
int main()
{
int num2 = 25;
_printf("Example4: Printing a octal of 25 as %o\n", num2);
return (0);
}Example4: Printing a octal of 25 as 31
We convert a decimal value to octal and replace the indetify format with the value stored in the variable num2.
int main()
{
char hash = '#';
_printf("Example5: Printing a hash character %c\n", hash);
return (0);
}Example5: Printing a hash character #
For the %c identy format, only We can use one character stored in a variable, no more, or a simple char caracter with simple quotes.
int main()
{
unsigned int num3 = 68;
_printf("Example6: The binary value of 68 is %b\n", num3);
return (0);
}Example6: The binary value of 68 is 1000100Example6: The binary value of 68 is 1000100
This identy format doesn't have the original function, converts a unsigned integer value to a binary and print it replacing the %b characters.
int main()
{
unsigned int num4 = 980;
_printf("Example7: Printing 980 as a hexadecimal %x, %X\n", num4, num4);
return (0);
}Example7: Printing 980 as a hexadecimal 3d4, 3D4
Print a unsigned integer as a hexadecimal number, with %x lowercase prints the base 16 letters in lowercase (abcdef) the %X uppercase prints in uppercase (ABCDEF).
Everything we need for the _printf function to execute correctly, we will show below:
We define a header file names "holberton.h" where puts the prototypes functions, include standard libraries to use and a struct that contains the different format types used in _printf function.
For some proccess is neccessary use the standard libraries for example we use the write function across the <unistd.h> library.
Small functions are necessary for realize some operations with the characters found in the process and are defined with their respective name, data type of parameters.
Contains a char parameter followed a function parameter who permits select the corresponding identify operators at the pointer position.
Contains the logic to use in each step the _printf process, using the different functions.
Made with ❤ by Cristian Pinzón。
Holberton School - Cohort 14 BOG Colombia 2021

