Tuesday, May 8, 2012


Implementation of printf() function


From the time I learned C programming properly, I was thinking how printf takes variable number of arguments. Here, is a small C program which gives a little bit of closer look.



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

char *convert (unsigned int num, int base) {
    static char buff[33];
    char *ptr;

    ptr=&buff[sizeof(buff)-1];
    *ptr='\0';

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

    return(ptr);
}

void myprintf(char * fmt, ...) {
    char *p;
    int i;
    unsigned u;
    char *s;
    char c;
    va_list argp;

    va_start(argp, fmt);

    p=fmt;
    for(p=fmt ; *p != '\0' ; p++) {
        if(*p == '%') {
            p++;

            switch(*p) {
                case 'c' :
                {
                    c=(char)va_arg(argp,int);
                    putchar(c);
                    break;
                }
                case 'd' :
                {
                    i=va_arg(argp,int);
                    if(i<0) {
                        i=-i;
                        putchar('-');
                    }
                    
                    puts(convert(i,10));
                    break;
                }
                case 'o':
                {
                    i=va_arg(argp,unsigned int);
                    puts(convert(i,8));
                    break;
                }
                case 's':
                {
                    s=va_arg(argp,char *);
                    puts(s);
                    break;
                }
                case 'u':
                {
                    u=va_arg(argp, unsigned int);
                    puts(convert(u,10));
                    break;
                }
                case 'x':
                {
                    u=va_arg(argp, unsigned int);
                    puts(convert(u,16));
                    break;
                }
            }
        } else {
            putchar(*p);
        }
    }

    va_end(argp);
}

int main () {
    int i;
    char str[200];
    printf("This an demo of how variable arguments works !!\n");
    printf("Enter any sequence of digits. It should get converted to respective Hexadecimal number : ");
    scanf("%d", &i);
    getchar();
    printf("Enter a string : ");
    scanf("%[^\n]", str);

    // [ ] are kept intentionally.
    myprintf("\nmyprintf: string = [%s], number = [%d] converted to hexadecimal = [%x]\n", str, i, i);

    /*
            $ gcc -Wall myPrint_program.c
            $ ./a.out

            This an demo of how variable arguments works !!
            Enter any sequence of digits. It should get converted to respective Hexadecimal number : 256
            Enter a string : How are u ?
            
            myprintf: string = [How are u ?
            ], number = [256
            ] converted to hexadecimal = [100
            ]
    */

    return 0;
}


How to run on Linux ?
$ gcc -Wall my_printf.c (this will generate a file named a.out)
$./a.out (this will execute the program)
 
 
If you get stuck at any function and its usage, refer man pages for it. ( do "man putchar" for example )

No comments:

Post a Comment