printf(0,"%d",num)中的0是做什么的?

Vic*_*ell 2 c printf arguments

我通常使用C++编写代码,但我正在使用C语言编写项目,并且遇到了一个带有以下语法的printf:

printf( 0, "%d\n", num);
Run Code Online (Sandbox Code Playgroud)

我环顾四周,无法找到printf中第一个0的解释.有人可以向我解释一下吗?谢谢.

Dav*_*eri 5

因为xv6没有使用printf标准库:

void
printf(int fd, char *fmt, ...)
{
    char *s;
    int c, i, state;
    uint *ap;
    state = 0;
    ap = (uint*)(void*)&fmt + 1;
    for(i = 0; fmt[i]; i++){
        c = fmt[i] & 0xff;
        if(state == 0){
            if(c == '%'){
                state = '%';
            } else {
                putc(fd, c);
            }
        } else if(state == '%'){
            if(c == 'd'){
                printint(fd, *ap, 10, 1);
                ap++;
            } else if(c == 'x' || c == 'p'){
                printint(fd, *ap, 16, 0);
                ap++;
            } else if(c == 's'){
                s = (char*)*ap;
                ap++;
                if(s == 0)
                    s = "(null)";
                while(*s != 0){
                    putc(fd, *s);
                    s++;
                }
            } else if(c == 'c'){
                putc(fd, *ap);
                ap++;
            } else if(c == '%'){
                putc(fd, c);
            } else {
            // Unknown % sequence. Print it to draw attention.
                putc(fd, '%');
                putc(fd, c);
            }
            state = 0;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • **哇**即使在**POSIX.1-2008**中指定了`dprintf()`,他们也真的将这个名称用于此类函数? (2认同)