Implementation of a function similar to printf
More...
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
Go to the source code of this file.
|
struct | buffer |
| struct used to store character in certain times More...
|
|
|
#define | INT_MAX_LENGTH 10 |
| for malloc and free functions for write function for va_start and va_arg functions
|
|
#define | PRECISION_FOR_FLOAT 8 |
|
|
typedef struct buffer | Buffer |
| struct used to store character in certain times
|
|
|
int | power_of_ten (int a) |
|
int | is_number (char *c) |
| Checks if a character is a number. More...
|
|
char | get_ch (char *p, Buffer *buffer) |
| Returns specific required next character. More...
|
|
void | unget_ch (char *c, Buffer *buffer) |
| Stores character to the buffer->buffr_char More...
|
|
int | get_number_of_digits (int n) |
| Calculates the number of digits in a number. More...
|
|
void | put_char (char s) |
| Prints one character on screen. More...
|
|
void | reverse_str (char *p) |
| Reverses a string using two pointer algorithm More...
|
|
void | print_int_value (int n, int width, int precision) |
|
void | print_double_value (double dval, int width, int precision) |
| The algorithm here is also the same as the print_int_value function. More...
|
|
void | print_string (char *p, int width, int precision) |
|
char * | get_width_and_precision (char *p, Buffer *buffer, int *width, int *precision) |
| Takes width and precision specified from the format of the string. More...
|
|
void | min_printf (char *fmt,...) |
| min_printf is the function same as printf More...
|
|
Implementation of a function similar to printf
printf
statement rewritten (as min_printf
) in C without using the stdio.h
library Syntax of min_printf
is same as printf
Currently min_printf handles: Integers, Doubles, floats, characters and strings The format specifiers and escape sequence is the same as for printf
User can also specify the width and precision if required, just like in the case of printf
How to use it:
◆ get_ch()
char get_ch |
( |
char * |
p, |
|
|
Buffer * |
buffer |
|
) |
| |
Returns specific required next character.
- Parameters
-
p | pointer to a format string of min_printf() |
buffer | struct for checking if buffr_char character is present or not |
- Returns
- character inside
buffer->buffr_char
, if buffer->buf_size
is one
-
character at which p is pointing, if
buffer->buf_size
is zero
< Since character is used, this sets buffer->buf_size
to zero
struct used to store character in certain times
Definition: min_printf.h:31
◆ get_number_of_digits()
int get_number_of_digits |
( |
int |
n | ) |
|
Calculates the number of digits in a number.
- Parameters
-
n | number whose digits are to be counted |
- Returns
- number of digits in n
◆ get_width_and_precision()
char * get_width_and_precision |
( |
char * |
p, |
|
|
Buffer * |
buffer, |
|
|
int * |
width, |
|
|
int * |
precision |
|
) |
| |
Takes width and precision specified from the format of the string.
- Parameters
-
p | pointer of the format string |
width | variable in which width will be stored |
precision | variable in which precision will be stored |
- Returns
- character pointer to the current pointer of string p (used to update value of p)
279 *width = *width * 10 + (*p++ -
'0');
284 *precision = *precision * 10 + (*p -
'0');
void unget_ch(char *c, Buffer *buffer)
Stores character to the buffer->buffr_char
Definition: min_printf.h:84
int is_number(char *c)
Checks if a character is a number.
Definition: min_printf.h:58
◆ is_number()
int is_number |
( |
char * |
c | ) |
|
Checks if a character is a number.
- Parameters
-
c | character to be checked if it's a number or not |
- Returns
true
(1) if the character is a number
-
false
(0) if the character is NOT a number
60 return (*c >=
'0' && *c <=
'9') ? 1 : 0;
◆ min_printf()
void min_printf |
( |
char * |
fmt, |
|
|
|
... |
|
) |
| |
min_printf is the function same as printf
- Parameters
-
fmt | format of string |
... | arguments passed according to the format |
308 for (p = fmt; *p !=
'\0'; ++p) {
325 ival = va_arg(ap,
int);
329 cval = va_arg(ap,
int);
333 dval = va_arg(ap,
double);
337 precision = PRECISION_FOR_FLOAT;
341 sval = va_arg(ap,
char *);
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
void print_int_value(int n, int width, int precision)
Definition: min_printf.h:154
char get_ch(char *p, Buffer *buffer)
Returns specific required next character.
Definition: min_printf.h:70
void print_double_value(double dval, int width, int precision)
The algorithm here is also the same as the print_int_value function.
Definition: min_printf.h:206
char * get_width_and_precision(char *p, Buffer *buffer, int *width, int *precision)
Takes width and precision specified from the format of the string.
Definition: min_printf.h:271
void put_char(char s)
Prints one character on screen.
Definition: min_printf.h:110
void print_string(char *p, int width, int precision)
Definition: min_printf.h:234
◆ power_of_ten()
int power_of_ten |
( |
int |
a | ) |
|
This function return ten to the power a(The parameter specified to it) like: if the parameter specified is 4 i.e. -> power_of_ten(4) is called then this function will return ten to the power four (10000);
- Parameters
-
a | The power of ten which is to be returned |
- Returns
- Ten to the power a
< This number will be returned as ten to power of a
47 for (
int i = 1; i <= a; ++i)
◆ print_double_value()
void print_double_value |
( |
double |
dval, |
|
|
int |
width, |
|
|
int |
precision |
|
) |
| |
The algorithm here is also the same as the print_int_value
function.
First, the digits before decimal is printed by converting the double to int. Then after printed a .
, the double number is subtracted with the integer value of the number, leaving us with 0 before the decimal. Then, we multiply the number with 10 raised to the power precision ( precision means how many digits to be printed after the decimal.) By default, the precision is 8 if it is not specified. Then, the remaining number is printed on the screen.
- Parameters
-
dval | double number to be printed |
width | similar to width parameter of print_int_value() |
precision | tells the number of digits to be printed after the decimal (By default it is 8) |
209 int reqd_blanks = width - (precision + 1) - ndigits;
218 dval = dval - (int) dval;
int power_of_ten(int a)
Definition: min_printf.h:44
int get_number_of_digits(int n)
Calculates the number of digits in a number.
Definition: min_printf.h:96
◆ print_int_value()
void print_int_value |
( |
int |
n, |
|
|
int |
width, |
|
|
int |
precision |
|
) |
| |
The algorithm here is to first convert the number into string and then reverse it be passing it to reverse_str function and then printing on the screen
- Parameters
-
n | Number to be printed |
width | Total characters to be printed (Prints ' ' if (size < width) |
precision | Total character of number to be printed (prints 0 before number if size of number < precision) |
< Used to store number of digits in number
The next two conditions check weather it is required to add blanks before printing the number (ie: width)and is it specified how many zeros to be printed before the number is printed (ie: precision)
176 if (width > 0 && size < width)
177 for (
int i = 0; i < (width - precision); ++i)
180 if (precision > 0 && precision > size)
181 for (
int i = 0; i < (precision - size); ++i)
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
void reverse_str(char *p)
Reverses a string using two pointer algorithm
Definition: min_printf.h:124
#define INT_MAX_LENGTH
for malloc and free functions for write function for va_start and va_arg functions
Definition: min_printf.h:25
◆ print_string()
void print_string |
( |
char * |
p, |
|
|
int |
width, |
|
|
int |
precision |
|
) |
| |
First size of the string is calculated to check whether width and precision are to be taken into account or not. Then, the string is printed in accordingly.
- Parameters
-
p | pointer to string to be printed |
width | if (width > sizeof string) then, blanks will be printed before sting to cover up the width |
precision | total characters of the string to be printed (prints the whole string if 0 or greater than size of string) |
251 if (precision != 0 && precision < size)
255 for (
int i = 0; i < (width - size); ++i)
259 for (
int i = 0; i < size; ++i)
◆ put_char()
Prints one character on screen.
- Parameters
-
s | character to be printed on the screen |
113 char *buf = (
char *)
malloc(
sizeof(
char) + 1);
◆ reverse_str()
void reverse_str |
( |
char * |
p | ) |
|
◆ unget_ch()
void unget_ch |
( |
char * |
c, |
|
|
Buffer * |
buffer |
|
) |
| |
Stores character to the buffer->buffr_char
- Parameters
-
c | character to be stored in the buffer->buffr_char |
buffer | struct where character will be stored |