#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
printf("a"+14);
printf("tera kya hoga kalia \n\n");
return 0;
}
//此代码打印
世界你好!
卡利亚
寺伽霍加卡利亚
//为什么要获取下一行的打印数据。
//是的,C 语言新手!!!
最佳答案
行:
printf("a"+14);
相当于:
char const* s1 = "a";
char const* s2 = s1 + 14;
printf(s2);
表达式s1 + 14
计算结果为无效指针。给定 s1
的值,仅 s1+0
和s1+1
是有效的指针。
自 s2
是一个无效的指针,该行
printf(s2);
是未定义行为的原因。
事实上,给定 s1
的值,只是计算s1+i
对于所有人来说都是未定义的行为i < 0
和i > 2
.
来自 EOF 的评论:
6.5.6 Additive operators 8 [...] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
关于c - 为什么在 C 中,像下面代码中提到的警告行会采用下一行的字符串并打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346772/