How to write my own printf () in C?

I'm actually trying to write my own printf()in C using varags. But I am not getting the right solution for this. Can anyone help me out?

+5
source share
5 answers

Before implementing the printf () function, we are dealing with an unusual problem, which is variable arguments. Since we know that printf can take many arguments besides a string. Therefore, we must use a standard library called stdarg.h to handle this problem with the variable argument. In this implementation context, we do not need to study the entire stdarg.h library because we use some macro functions from this library that are directly understood by our C program.

Here is a source of code that explains good and fast

#include<stdio.h> 
#include<stdarg.h>                      

void Myprintf(char *,...);              //Our printf function
char* convert(unsigned int, int);       //Convert integer number into octal, hex, etc.


int main() 
{ 
    Myprintf(" WWW.FIRMCODES.COM \n %d", 9); 

    return 0;
} 


void Myprintf(char* format,...) 
{ 
    char *traverse; 
    unsigned int i; 
    char *s; 

    //Module 1: Initializing Myprintf arguments 
    va_list arg; 
    va_start(arg, format); 

    for(traverse = format; *traverse != '\0'; traverse++) 
    { 
        while( *traverse != '%' ) 
        { 
            putchar(*traverse);
            traverse++; 
        } 

        traverse++; 

        //Module 2: Fetching and executing arguments
        switch(*traverse) 
        { 
            case 'c' : i = va_arg(arg,int);     //Fetch char argument
                        putchar(i);
                        break; 

            case 'd' : i = va_arg(arg,int);         //Fetch Decimal/Integer argument
                        if(i<0) 
                        { 
                            i = -i;
                            putchar('-'); 
                        } 
                        puts(convert(i,10));
                        break; 

            case 'o': i = va_arg(arg,unsigned int); //Fetch Octal representation
                        puts(convert(i,8));
                        break; 

            case 's': s = va_arg(arg,char *);       //Fetch string
                        puts(s); 
                        break; 

            case 'x': i = va_arg(arg,unsigned int); //Fetch Hexadecimal representation
                        puts(convert(i,16));
                        break; 
        }   
    } 

    //Module 3: Closing argument list to necessary clean-up
    va_end(arg); 
} 

char *convert(unsigned int num, int base) 
{ 
    static char Representation[]= "0123456789ABCDEF";
    static char buffer[50]; 
    char *ptr; 

    ptr = &buffer[49]; 
    *ptr = '\0'; 

    do 
    { 
        *--ptr = Representation[num%base]; 
        num /= base; 
    }while(num != 0); 

    return(ptr); 
}
+8
source

, GNU libc: . printf, , , vprintf, vfprintf

+6

Linux va_start (3) ( , ). libstdc.

+3

, printf() ( ):

  • Plauger, ' C
  • Hanson, C '
+2

, . , / , , , , printf().

, .

( ):

#include <stdarg.h>
void _printf(FILE *out, va_list ap)
{
    vfprintf(out, fmt, ap);
}

void printf(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    _printf(stdout, ap);
    va_end(ap);
}

: ( ) . , , va_start va_end, , va_list , .

I highly recommend looking at the implementation of the printf BSD (or even glibc) subsystem. You can also look at uclibc, dietlibc, etc.

0
source

All Articles