实现自己的变参函数printf

在c/c++标准库中,变参函数很特别。printf, fprintf, sprintf等都属于变参函数。如果自己要写类似的参数可变的函数,通常会用到下面三个函数:


#include <stdarg.h>

       void va_start(va_list ap, last);
       type va_arg(va_list ap, type);
       void va_end(va_list ap);


一般的用法是这样:
va_list args; //声明变量
va_start(args, fmt); //开始解析。args指向fmt后面的参数
TYPE var = va_arg(args, TYPE); //取下一个参数并返回。args指向下一个参数
va_end(args); //结束解析

下面就给出具体的例子:

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

/*
 * Conver int to string based on radix (usually 2, 8, 10, and 16)
 */

char *itoa(int num, char *str, int radix)
{
    char string[] = "0123456789abcdefghijklmnopqrstuvwxyz";

    char* ptr = str;
    int i;
    int j;

    while (num)
    {
        *ptr++ = string[num % radix];
        num /= radix;

        if (num < radix)
        {
            *ptr++ = string[num];
            *ptr = '\0';
            break;
        }
    }

    j = ptr - str - 1;

    for (i = 0; i < (ptr - str) / 2; i++)
    {
        int temp = str[i];
        str[i] = str[j];
        str[j--] = temp;
    }

    return str;
}

/*
 * A simple printf function. Only support the following format:
 * Code Format
 * %c character
 * %d signed integers
 * %i signed integers
 * %s a string of characters
 * %o octal
 * %x unsigned hexadecimal
 */

int my_printf( const char* format, ...)
{
    va_list arg;
    int done = 0;

    va_start (arg, format);
    //done = vfprintf (stdout, format, arg);

    while( *format != '\0')
    {
        if( *format == '%')
        {
            if( *(format+1) == 'c' )
            {
                char c = (char)va_arg(arg, int);
                putc(c, stdout);
            } else if( *(format+1) == 'd' || *(format+1) == 'i')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 10);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 'o')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 8);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 'x')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 16);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 's' )
            {
                char* str = va_arg(arg, char*);
                while( *str != '\0') putc(*str++, stdout);
            }

            // Skip this two characters.

            format += 2;
        } else {
            putc(*format++, stdout);
        }
    }

    va_end (arg);

    return done;
}

int main(int argc, char* argv[])
{
    int n = 255;
    char str[] = "hello, world!";

    printf("n = %d\n", n);
    printf("n = %i\n", n);
    printf("n = %o\n", n);
    printf("n = %x\n", n);
    printf("first char = %c\n", str[0]);
    printf("str = %s\n", str);
    printf("%s\tn = %d\n", str, n);

    // Test my_printf function

    printf("---------------my_printf--------------\n");
    
    my_printf("n = %d\n", n);
    my_printf("n = %i\n", n);
    my_printf("n = %o\n", n);
    my_printf("n = %x\n", n);
    my_printf("first char = %c\n", str[0]);
    my_printf("str = %s\n", str);
    my_printf("%s\tn = %d\n", str, n);

    return 0;
}


编译运行,结果如下:

[ray@localhost c++]$ gcc -o printf printf.c
[ray@localhost c++]$ ./printf
n = 255
n = 255
n = 377
n = ff
first char = h
str = hello,
hello, n = 255
---------------my_printf--------------
n = 255
n = 255
n = 377
n = ff
first char = h
str = hello,
hello, n = 255

转自:http://www.cublog.cn/u2/68938/showart_726427.html

你可能感兴趣的:(printf)