Skip to content
faykrisPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

README.md

Description

  • Function that prints argument(s) according to format

Features

  • 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]

How It Works?

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.

_printf function:

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:

Print a text Without variables

#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

Print some concatenated strings

#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.

Only a text with variables

#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

Print a integer number

#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.

Print a decimal to octal number

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.

Print a char character

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.

Print a binary number

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.

Print a hexadecimal number

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).

How My Code Works?

Everything we need for the _printf function to execute correctly, we will show below:

Header file

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.

Standard libraries

For some proccess is neccessary use the standard libraries for example we use the write function across the <unistd.h> library.

Functions prototypes

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.

cp_func struct

Contains a char parameter followed a function parameter who permits select the corresponding identify operators at the pointer position.

.c Source codes

Contains the logic to use in each step the _printf process, using the different functions.

Contributors

Made with ❤ by Cristian Pinzón。

Holberton School - Cohort 14 BOG Colombia 2021

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages