I'm a complete noob at C and i need some help understanding why a certain piece of code compiles correctly.
main(){
char name[3];
strcpy(name, "12345678912312");
printf("%s\n",name);
}
So this code compiles correctly;however, I don't understand why it does not cause a segmentation fault. From my understanding of c, each character is 1 byte. The array name, is supposed to be able to hold 3 bytes, instead it can hold a lot more than that. Why is that?
Additionally, if I add one more character to this, I will get Illegal Instruction(core dumped).
main(){
char name[3];
strcpy(name, "123456789123121");
printf("%s\n",name);
}
Then if I add another character to that code, it will throw a Segmentation fault (core dumped) error. Why are the errors different? And why did they not occur before?
And lastly, where can I find documentation for each function? I’m coming from java so I am used to referencing to the java docs.
Im using GCC compiler in Ubuntu linux.