Coder Social home page Coder Social logo

holbertonschool-printf's Introduction

holbertonschool-printf

Implements a custom printf for C program completed as part of the low-level programming and algorithm track at Holberton School. The program is a partial recreation of the C standard library function, printf.

Dependencies ๐Ÿ‘ซ

The _printf function was coded on an Ubuntu 20.04 LTS machine with gcc version 10.2.1.

Usage ๐Ÿƒ

To use the _printf function, assuming the above dependencies have been installed, compile all .c files in the repository and include the header main.h with any main function.

Example main.c:

#include "main.h"

int main(void)
{
    _printf("Hello, World!");

    return (0);
}

Compilation:

$ gcc *.c -o tester

Output:

$ ./tester
Hello, World!
$

Description ๐Ÿ’ฌ

The function _printf writes output to standard output. The function writes under the control of a format string that specifies how subsequent arguments (accessed via the variable-length argument facilities of stdarg) are converted for output.

Prototype: int _printf(const char *format, ...);

Return Value

Upon successful return, _printf returns the number of characters printed (excluding the terminating null byte used to end output to strings). If an output error is encountered, the function returns -1.

Format of the Argument String

The format string argument is a constant character string composed of zero or more directives: ordinary characters (not %) which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Conversion specification is introduced by the character % and ends with a conversion specifier.

Conversion Specifiers

The conversion specifier (introduced by the character %) is a character that specifies the type of conversion to be applied. The _printf function supports the following conversion specifiers:

d, i

The int argument is converted to signed decimal notation.

Example main.c:

int main(void)
{
    _printf("%d\n", 7);
}

Output:

7

c

The int argument is converted to an unsigned char.

Example main.c:

int main(void)
{
    _printf("%c\n", 'c');
}

Output:

c

s

The const char * argument is expected to be a pointer to a character array (aka. pointer to a string). Characters from the array are written starting from the first element of the array and ending at, but not including, the terminating null byte (\0).

Example main.c:

int main(void)
{
    _printf("%s\n", "Hello, World!");
}

Output:

Hello, World!

%

A % is written. No argument is converted. The complete conversion specification is %%.

Example:

int main(void)
{
    _printf("%%\n");
}

Output:

%

More Examples ๐Ÿ‘

To print the address of Holberton School in the format "972 Mission St., San Francisco, CA 94103" where street, city and state are pointers to strings:

Example main.c:

#include "holberton.h"

int main(void)
{
	char *street = "Mission St.", *city = "San Francisco", *state = "CA";

	_printf("%d %s, %s, %s %d\n", 972, street, city, state, 94103);
}

Output:

972 Mission St., San Francisco, CA 94103

To print the result of basic mathematical operations prepended by signs and all numbers printed with a minimum precision of two digits:

Example main.c:

#include "holberton.h"

int main(void)
{
	_printf("%.2d + %.2d = %+.2d\n", 1, 2, 1 + 2);
	_printf("%d - %d = %+d\n", 10, 20, 10 - 20);
}

Output:

01 + 02 = +03
10 - 20 = -10

Authors โœ’๏ธ

License ๐Ÿ”’

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements ๐Ÿ™

The _printf function emulates functionality of the C standard library function printf. This README borrows from the Linux man page printf(3).

This program was written as part of the curriculum for Holberton School. Holberton School is a campus-based full-stack software engineering program that prepares students for careers in the tech industry using project-based peer learning. For more information, visit this link.

Holberton School logo

holbertonschool-printf's People

Contributors

zonafrank avatar warrentherabbit avatar tingru0515 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.