c - 无法理解 C 代码的输出

标签 c

<分区>

#include<stdio.h>

int main()
{
    int a=5;
    printf("%d \n"+1,a);   // output : d
    printf("%%d \n"+1,a);  // output : 5
    printf("%q \n"+1,a);   // output : q
    printf("%%q \n"+1,a);  // output : q
    printf("%k \n"+1,a);   // output : k
    printf("%%k \n"+1,a);  // output : k
    printf("ABCD \n");     // output : ABCD
    printf("ABCD \n" +1);  // output : BCD
    printf("ABCD \n" +3);  // output : D
}

我无法理解上述程序给出的输出。我是新手。可能是如果我必须了解指针中的高级主题??

最佳答案

"%d\n"+1 返回指向包含 "d\n" 的空间的指针。同样,"%%d\n"+1 返回指向 "%d\n" 的指针。

因此,声明

printf("%%d \n"+1,a);  // output : 5

相当于

printf("%d \n", a);

这就是它输出 5 的原因。

至于"%%q\n"+1等同于%q\n%q不是预定义的格式说明符,并且行为未定义(请参阅 C1X 第 315 页)。

其他陈述的答案类似。

关于c - 无法理解 C 代码的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24979380/

相关文章:

c - 如何跟踪或调试GMP?

c - 为什么我的字符串在 C 语言中显示美元符号

c - 8位二进制0减1

c - 为什么用GCC编译的程序不能用DOSbox运行而用Borland(Turbo C)编译的程序可以?

c - 如何在函数中使用链接列表

c++ - 代码覆盖率不好

C 代码因 memmove 崩溃

c++ - 微小的 SSE addpd 循环比 AMD Phenom II 上的标量稍慢?

c - 写入 c 字符串

c++ - C/C++ : print contents of (config) header file