I wrote this incredibly stupid code
#include <stdio.h>
main(){
int new[10], i;
for(i=1; i<=10; ++i){
new[i] = 0;
}
for(i=1;i<=10; ++i)
{
printf("%d", new[i]);
}
}
I compiled this using GCC on Xubuntu and then did the ./a.out. The cursor is just blinking resulting in no output. The same is the case when tried to debug with gdb. It runs and then stays with the blinking cursor.
Any help?
C arrays are 0 indexed - your program writes outside the boundaries of the new array, so it causes undefined behaviour. In this case, you probably are overwriting the i variable, so you end up with an infinite loop. You need to change your loops:
for (i = 0; i < 10; i++)
{
new[i] = 0;
}
and:
for (i = 0; i < 10; i++)
{
printf("%d", i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With