有人可以向我解释给定代码中发生了什么吗?

标签 c io printf

   #include<stdio.h>
    int main()
    {
        char s[]="chomsky the great";
        printf("try0 %s\n",s+s[3]-s[9]);    
        printf("try1 %s\n",s+s[3]-s[1]);
        return 0;
    }   

gcc 编译器中的 o/p 是

        try0 ky the great
        try1 ky the great

我无法跟踪程序在这里实际做了什么,或者编译器是如何工作的。

最佳答案

s+s[3]-s[9] = s + *(s+3) - *(s+9) = s + 'm'- 'h' = s + 109 - 104 =  s + 5 = s[5] 

所以 printf 从 s[5] 开始打印

printf 打印的例子:

printf("%s",s) = chomsky the great

printf("%s",s[0]) = chomsky the great

printf("%s",s[2]) = omsky the great

printf("%s",s[5]) = ky the great

关于有人可以向我解释给定代码中发生了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693844/

相关文章:

c - 使用 C 使用 pic16f877a 同时运行多个函数

haskell - getLine 是懒惰的吗?

docker - 错误:IO::Pipe:无法执行:没有这样的文件或目录

java - 拖尾文件时java中的巨大虚拟内存

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

c++ - 在 Debug模式下运行时缓冲区溢出

c - 在不使用内存操作方法(malloc...等)的情况下初始化c中的结构

c - C中这个奇怪的函数定义语法是什么?

c - 在 Eclipse 中将 fgets(char* c, int i, file* f) 与 printf() 结合使用 - CDT。输出顺序不正确。!

c - 如何使用c将目录的文件名添加到数组结构中