C语言中可变参数打印代码解读

可变参数打印

使用C语言的同学都知道,C语言的打印函数printf();是可以打印可变参数的,比如:

printf("hello world!My name is %s,I am %d years old\n","Jack",98);
printf("hello world!My name is %s\n","Jack");

对于计算机程序来说,总是一行行执行代码,对于C语言来说,可变参数是如何实现的呢? 

可变参数实现的知识点

对于可变参数的实现,首先是函数调用栈的入栈顺序,以及基于栈的参数寻址。第二个知识点就是对每个字符使用系统调用进行打印。

【函数调用栈】

不管有多少个可变参数,当我们写完打印语句的时候,参数个数和位置就已经确定了。当我们写下如下语句时候,经过C语言编译后,参数放入栈中。

printf("my name is %s,I am %d years old",
"xiaoxiao",
10);

既然所有的参数已经放入栈的数据结构中,那么我们只需要设计一种算法,对栈中的元素进行某种方式的遍历,从而把我们希望的打印结果按顺序显示即可。

C语言的<stdarg.h>提供3种能够对栈进行遍历的API。

typedef __builtin_va_list va_list;
#define va_start(ap, param) __builtin_va_start(ap, param)
#define va_end(ap)          __builtin_va_end(ap)
#define va_arg(ap, type)    __builtin_va_arg(ap, type)

说明:

一般va_list是char *的指针。

va_start(ap, param):入参:param,它一般是第一个可变参数的地址;出参:ap,指向第一个可变参数的地址。

va_arg(ap, type):入参:ap是前一个参数的首地址,该函数使得ap能够加1,指向下一个参数的首地址;type,是ap指向的参数的类型。该函数将解析ap指向的参数

va_end:释放指针,将输入的参数 ap 置为 NULL。通常va_start和va_end是成对出现。 

举个例子:

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
/*ANSI标准形式的声明方式,括号内的省略号表示可选参数*/
int demo( char *msg,...)
{
    /*定义保存函数参数的结构*/
    va_list argp;
    char *para;

    va_start(argp, msg);
    printf("parameter1=%s\n",msg);
    para = va_arg( argp, char*);
    printf("parameter2=%s\n",para);
    va_end(argp);
    return 0;
}
int main()
{
    demo("DEMO", "demo");
    return 0;
}

输出结果:

parameter1=DEMO
parameter2=demo

【系统调用】

通过某种算法,组合使用va_start、va_arg、va_end这样的API,就能够按格式输出想要的打印结果。但是对于输出而言,程序需要调用系统API进行字符串显示。

printf的内部底层采用putchar(),用来输出一个字符的系统调用,这样来保证整个字符串输出正确。

cprintf可变参数实现源代码

这份源代码实现了完整的printf打印,它包含一个头文件和一个源文件。

error.h

#ifndef __LIBS_ERROR_H__
#define __LIBS_ERROR_H__

/* kernel error codes -- keep in sync with list in lib/printfmt.c */
#define E_UNSPECIFIED       1   // Unspecified or unknown problem
#define E_BAD_PROC          2   // Process doesn't exist or otherwise
#define E_INVAL             3   // Invalid parameter
#define E_NO_MEM            4   // Request failed due to memory shortage
#define E_NO_FREE_PROC      5   // Attempt to create a new process beyond
#define E_FAULT             6   // Memory fault
#define E_SWAP_FAULT        7   // SWAP READ/WRITE fault
#define E_INVAL_ELF         8   // Invalid elf file
#define E_KILLED            9   // Process is killed
#define E_PANIC             10  // Panic Failure
#define E_TIMEOUT           11  // Timeout
#define E_TOO_BIG           12  // Argument is Too Big
#define E_NO_DEV            13  // No such Device
#define E_NA_DEV            14  // Device Not Available
#define E_BUSY              15  // Device/File is Busy
#define E_NOENT             16  // No Such File or Directory
#define E_ISDIR             17  // Is a Directory
#define E_NOTDIR            18  // Not a Directory
#define E_XDEV              19  // Cross Device-Link
#define E_UNIMP             20  // Unimplemented Feature
#define E_SEEK              21  // Illegal Seek
#define E_MAX_OPEN          22  // Too Many Files are Open
#define E_EXISTS            23  // File/Directory Already Exists
#define E_NOTEMPTY          24  // Directory is Not Empty
/* the maximum allowed */
#define MAXERROR            24

#endif /* !__LIBS_ERROR_H__ */

main.c

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

static const char * const error_string[MAXERROR + 1] = {
    [0]                     NULL,
    [E_UNSPECIFIED]         "unspecified error",
    [E_BAD_PROC]            "bad process",
    [E_INVAL]               "invalid parameter",
    [E_NO_MEM]              "out of memory",
    [E_NO_FREE_PROC]        "out of processes",
    [E_FAULT]               "segmentation fault",
    [E_INVAL_ELF]           "invalid elf file",
    [E_KILLED]              "process is killed",
    [E_PANIC]               "panic failure",
};

#define do_div(n, base) ({                                          \
            unsigned long __upper, __low, __high, __mod, __base;    \
            __base = (base);                                        \
            asm ("" : "=a" (__low), "=d" (__high) : "A" (n));       \
            __upper = __high;                                       \
            if (__high != 0) {                                      \
                __upper = __high % __base;                          \
                __high = __high / __base;                           \
            }                                                       \
            asm ("divl %2" : "=a" (__low), "=d" (__mod)             \
                : "rm" (__base), "0" (__low), "1" (__upper));       \
            asm ("" : "=A" (n) : "a" (__low), "d" (__high));        \
            __mod;                                                  \
        })

unsigned long long
getuint(va_list *ap, int lflag);

long long
getint(va_list *ap, int lflag);

void
printnum(void (*putch)(int, void*), void *putdat,
        unsigned long long num, unsigned base, int width, int padc);

void
printfmt(void (*putch)(int, void*), void *putdat, const char *fmt, ...);

void
cputch(int c, int *cnt);

void
vprintfmt(void (*putch)(int, void*), void *putdat, const char *fmt, va_list ap);

int
vcprintf(const char *fmt, va_list ap);

int
cprintf(const char *fmt, ...);

int main()
{
    cprintf("hello world!My name is %s,I am %d years old\n","Jack",98);
    printf("hello world!My name is %s\n","Jack");
    return 0;
}

void
vprintfmt(void (*putch)(int, void*), void *putdat, const char *fmt, va_list ap) {
    register const char *p;
    register int ch, err;
    unsigned long long num;
    int base, width, precision, lflag, altflag;

    while (1) {
        while ((ch = *(unsigned char *)fmt ++) != '%') {
            if (ch == '\0') {
                return;
            }
            putch(ch, putdat);
        }

        // Process a %-escape sequence
        char padc = ' ';
        width = precision = -1;
        lflag = altflag = 0;

    reswitch:
        switch (ch = *(unsigned char *)fmt ++) {

        // flag to pad on the right
        case '-':
            padc = '-';
            goto reswitch;

        // flag to pad with 0's instead of spaces
        case '0':
            padc = '0';
            goto reswitch;

        // width field
        case '1' ... '9':
            for (precision = 0; ; ++ fmt) {
                precision = precision * 10 + ch - '0';
                ch = *fmt;
                if (ch < '0' || ch > '9') {
                    break;
                }
            }
            goto process_precision;

        case '*':
            precision = va_arg(ap, int);
            goto process_precision;

        case '.':
            if (width < 0)
                width = 0;
            goto reswitch;

        case '#':
            altflag = 1;
            goto reswitch;

        process_precision:
            if (width < 0)
                width = precision, precision = -1;
            goto reswitch;

        // long flag (doubled for long long)
        case 'l':
            lflag ++;
            goto reswitch;

        // character
        case 'c':
            putch(va_arg(ap, int), putdat);
            break;

        // error message
        case 'e':
            err = va_arg(ap, int);
            if (err < 0) {
                err = -err;
            }
            if (err > MAXERROR || (p = error_string[err]) == NULL) {
                printfmt(putch, putdat, "error %d", err);
            }
            else {
                printfmt(putch, putdat, "%s", p);
            }
            break;

        // string
        case 's':
            if ((p = va_arg(ap, char *)) == NULL) {
                p = "(null)";
            }
            if (width > 0 && padc != '-') {
                for (width -= strnlen(p, precision); width > 0; width --) {
                    putch(padc, putdat);
                }
            }
            for (; (ch = *p ++) != '\0' && (precision < 0 || -- precision >= 0); width --) {
                if (altflag && (ch < ' ' || ch > '~')) {
                    putch('?', putdat);
                }
                else {
                    putch(ch, putdat);
                }
            }
            for (; width > 0; width --) {
                putch(' ', putdat);
            }
            break;

        // (signed) decimal
        case 'd':
            num = getint(&ap, lflag);
            if ((long long)num < 0) {
                putch('-', putdat);
                num = -(long long)num;
            }
            base = 10;
            goto number;

        // unsigned decimal
        case 'u':
            num = getuint(&ap, lflag);
            base = 10;
            goto number;

        // (unsigned) octal
        case 'o':
            num = getuint(&ap, lflag);
            base = 8;
            goto number;

        // pointer
        case 'p':
            putch('0', putdat);
            putch('x', putdat);
            num = (unsigned long long)(uintptr_t)va_arg(ap, void *);
            base = 16;
            goto number;

        // (unsigned) hexadecimal
        case 'x':
            num = getuint(&ap, lflag);
            base = 16;
        number:
            printnum(putch, putdat, num, base, width, padc);
            break;

        // escaped '%' character
        case '%':
            putch(ch, putdat);
            break;

        // unrecognized escape sequence - just print it literally
        default:
            putch('%', putdat);
            for (fmt --; fmt[-1] != '%'; fmt --)
                /* do nothing */;
            break;
        }
    }
}

int
vcprintf(const char *fmt, va_list ap) {
    int cnt = 0;
    vprintfmt((void*)cputch, &cnt, fmt, ap);
    return cnt;
}

int
cprintf(const char *fmt, ...) {
    va_list ap;

    va_start(ap, fmt);
    int cnt = vcprintf(fmt, ap);
    va_end(ap);

    return cnt;
}

void
cputch(int c, int *cnt) {
    putchar(c);
    (*cnt) ++;
}

void
printfmt(void (*putch)(int, void*), void *putdat, const char *fmt, ...) {
    va_list ap;

    va_start(ap, fmt);
    vprintfmt(putch, putdat, fmt, ap);
    va_end(ap);
};

unsigned long long
getuint(va_list *ap, int lflag) {
    if (lflag >= 2) {
        return va_arg(*ap, unsigned long long);
    }
    else if (lflag) {
        return va_arg(*ap, unsigned long);
    }
    else {
        return va_arg(*ap, unsigned int);
    }
}

long long
getint(va_list *ap, int lflag) {
    if (lflag >= 2) {
        return va_arg(*ap, long long);
    }
    else if (lflag) {
        return va_arg(*ap, long);
    }
    else {
        return va_arg(*ap, int);
    }
}

void
printnum(void (*putch)(int, void*), void *putdat,
        unsigned long long num, unsigned base, int width, int padc) {
    unsigned long long result = num;
    unsigned mod = do_div(result, base);

    // first recursively print all preceding (more significant) digits
    if (num >= base) {
        printnum(putch, putdat, result, base, width - 1, padc);
    } else {
        // print any needed pad characters before first digit
        while (-- width > 0)
            putch(padc, putdat);
    }
    // then print this (the least significant) digit
    putch("0123456789abcdef"[mod], putdat);
};

输出结果:

hello world!My name is Jack,I am 98 years old
hello world!My name is Jack


版权声明:本文为u012750235原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。