c - 如何使用整数打印 NULL 字符串

标签 c printf

我有以下代码

#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/

相关文章:

C:动态格式说明符

编译 C 程序

c++ - 从旧书中学习c++

c - 文件内容不会读入结构

c - printf 字符打印的意外结果

c - "backspace"转义字符 '\b' : unexpected behavior?

c - 如何从 IDE 编译 CHOLMOD 库 (SuiteSparse)

c - 时间复杂度划分

c++ - GCC 中的 PRIuPTR 预处理器错误?

c - 了解 C 数组和指针