I have allocated space only for 3 characters. But y I could print more than 3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I have allocated space only for 3 characters. But y I could print more than 3

char a[3]; gets (a) ; printf ("%s", a) ;

3rd Dec 2018, 9:06 AM
Mohamed Burhan
Mohamed Burhan - avatar
3 Answers
+ 4
This array gives contigous memory of 3 but it is still possible that a[3] which is the memory location just after a[2] is available so you are able to use it. Conclusion: It is a matter of chance that a[3], a[4], a[5] etc. can be used by your code.
3rd Dec 2018, 9:55 AM
Lakshay
Lakshay - avatar
+ 1
Thanks got you all๐Ÿ˜Š๐Ÿ‘
3rd Dec 2018, 10:43 AM
Mohamed Burhan
Mohamed Burhan - avatar