gpt4 book ai didi

c - printf(0, "%d", num) 中的 0 有什么作用?

转载 作者:行者123 更新时间:2023-12-02 06:57:14 24 4
gpt4 key购买 nike

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

printf( 0, "%d\n", num);

我环顾四周,找不到关于 printf 中第一个 0 的作用的解释。有人可以向我解释一下吗?谢谢。

最佳答案

因为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;
}
}
}

关于c - printf(0, "%d", num) 中的 0 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28903853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com