Giter Club home page Giter Club logo

printf's Introduction

printf - a rudimentary implementation

Description

printf is a complex function that parses a format string for certain specifiers and essentially pastes content\n from arguments that match the specifier. Each specifier corresponds to the a specific argument that matches that type. Print regular characters as is, but print an argument when a directive (%) appears in the format string. The goal of the project was to match the functionality of printf and we've made some progress towards that goal. Further work is needed to implement the WiP specifiers.

Function prototype

  • int _printf(const char *format, ...)
  • _printf is a variadic function which takes in a variable number of arguments requiring macros such as (va_start, va_end,va_arg, va_list) to print such arguments.

Specifiers included

  • %c specifier prints a character to stdout using the write function

  • %s prints a character string by counting length of string and writes 'length' many bytes to stdout

  • %d, %i prints an integer one digit at a time

  • %b takes an unsigned int and converts it to its binary representation Potential bugs: passing in a string argument prints some binary representation that we were not able to fully understand.

  • %r prints a string in reverse

  • %R using the rot13 cipher, converts each character to the appropriate character from the rot13 string

WiP Specifiers

  • u, o, x, X
  • S
  • p
  • l
  • h

WiP Flag characters

  • space
  • 0

Included functions

Each function called, returns the number of characters printed to stdout.

int (*getfunc(char s))(va_list)

Function pointer takes in a specifier in the form of a char and returns the associated function to print an argument of that type. Traverses array of structs containing 2 members: the character specifier and the function to print that type. Each function called returns the numbers printed to stdout.

int _negativeone(__attribute__((unused)) va_list valist)

Function that returns a failure when a specifier is not located within the array of structs. Since all second members of a struct point to a function that takes in a va_list, thi s attribute is unused, so it needs to return a fail state of -1.

int _percent(__attribute__((unused)) va_list valist)

Prints a percent if that character after the % is a %.

int _printbinary(va_list valist)

It's easy to think about the individual characters of the resultant binary representation as a bunch of characters consisting of 0s and 1s, so we've allocated memory of 60 bytes for such chars. The size of an unsigned int varies from machine to machine, our development environment consists of Ubuntu 14.04 64 bit, so an unsigned int is 4 bytes. Meaning that there are 32 possible bits, and we allocate at least double that just to be on the safe side.

With this memory allocated, push characters into positions in memory in the order that they appear in the calculations and append a null byte at the end.

Use this null byte to our advantage, by calculating length and printing the string in reverse.

Authors

Name Github Link Twitter
Marius Farioletti https://github.com/thrownblown https://twitter.com/thrownblown
Hemant Heer https://github.com/VortexH https://twitter.com/hemant_heer

printf's People

Contributors

thrownblown avatar vortexh avatar

Watchers

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