我有以下代码
#include<stdio.h>
#include<string.h>
int main (void)
{
unsigned int pqr = 201;
unsigned int def = 113;
printf("The values are,%u,%s,%s,%u\n", pqr,"\0","\0",def);
printf("The values are,%u,%d,%d,%u\n", pqr,atoi("\0"),atoi("\0"),def);
}
值为,201,,,113
值为,201,0,0,113
如果值字符串设置为 NULL 并且我使用了格式说明符“%S”,则第一个 printf 不会给我任何结果。如何在第二个中使用格式说明符 %d 获得相同的结果。目前它给出“0”而不是什么都没有。
最佳答案
The first printf gives me nothing if the value string is set to NULL and I used format specifier "%S".
""
是空字符串。 "\0"
是一个带有附加空字符的空字符串。它与 NULL
不同。为 s
转换说明符传递空指针是未定义的行为。
How do I achieve the same result using format specifier %d in the second.
你不能。 printf
与 %d
转换规范打印一个 int
值;它不能打印任何内容。
关于c - 如何使用整数打印 NULL 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902549/