我通常使用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;
}
}
}
dprintf()
? - Iharob Al Asimi这是未定义行为;我猜你可能会遇到分段错误。
NULL
指针是未定义行为,因此任何行为都是可能的,因为它是未定义的,它可能是段错误,也可能正常工作,或者其他任何行为,它是未定义的... - Iharob Al Asimiwrite(0 ...
那还好,但在这种情况下,OP传递了0
作为格式字符串,在printf()
中将被解引用而没有检查其是否为NULL
。 - Iharob Al Asimiprintf( 0, "%d\n", num);
调用未定义行为,因为 printf
的第一个参数必须是指向字符串的指针。
[警告] 空参数在需要非空参数的位置 (参数 1) [-Wnonnull]
和[警告] 格式中的参数过多 [-Wformat-extra-args]
- Rizier123fprintf
吗? - Paul Rfprintf()
,也是同样的(FILE *)0
...除非它是dprintf()
,该函数会将输出打印到文件描述符并在 POSIX.1-2008 中指定。 - Iharob Al Asimi