问题描述
#include<stdio.h>
int main()
{
char s[2]="a";
s[1]='b';s[2]='c';s[3]='d';s[5]='e';
printf("%s $%c$",s,s[4]);
return 0;
}
1。当我运行这个程序在C(GCC-4.7.2)我预料之中,因为缺少NULL字符的运行时错误('\\ 0')
1.When I run this program in C (gcc-4.7.2) I expected Runtime Error because of the missing Null Character ('\0').
2。如果仍然是程序编译和成功执行,因为S [4]没有被初始化,我预计一些垃圾值place..but这里也是我错了。
2.If still the program compiles and executes successfully ,since s[4] has not been initialised,I expected some garbage value at that place..but here also I was wrong.
上述程序的输出是:
ABCDE $$
有两个$(美金)之间没有任何字符,表示跳过的printf S [4]。
这里是一个ideone链接相同的:
解释原因行为(输出)?
Explain the reason for this behaviour (output) ?
推荐答案
访问越界的数组是未定义的行为。只是举个例子一样code是我的系统上的输出是 ABCD(e▒x$($
Accessing out of bound of an array is undefined behaviour. Just an example same code's output on my system is abcd(e▒x $($
长度为8的字符串是因为缺少 $
之间NULL终止和字符(
的是垃圾值 S [4]
。
string of length 8 is because of lack of NULL terminator and character (
between $
is garbage value of s[4]
.
这篇关于C字符串程序的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!