Output of a C string program

前端 未结 3 490
说谎
说谎 2020-12-12 01:12
#include
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;
 }

相关标签:
3条回答
  • 2020-12-12 01:34
    1. It isn't necessary for a runtime error to occur. C does no bound checking.
    2. There are many characters defined in C. Like the sound beep \a if I remebember correct so it isn't necessary that something is actually printed on the screen. It might have been a sound that you never heard.
    0 讨论(0)
  • 2020-12-12 01:41

    Accessing out of bound of an array is undefined behaviour. Just an example same code's output on my system is abcd(e▒x $($

    string of length 8 is because of lack of NULL terminator and character ( between $ is garbage value of s[4].

    0 讨论(0)
  • 2020-12-12 01:44

    You are writing/reading outside of the bounds of the array, this is simply undefined behavior you can not make any predictions about what the program will do.

    0 讨论(0)
提交回复
热议问题