I am learning programming on my own. I came across arrays and tried to alter the for loop's condition from i < 5 to i <=5 , the outcome was unexpected, infinite loop:
#include <stdio.h>
int main(void)
{
int a[5] , i , j = 0;
for (i=0;i<=5;i++)
{
a[i] = 2;
printf("%d " , a[5]);
}
return 0;
}
Again I tried the same but this time with a variable , but this time no infinite loop.
#include <stdio.h>
int main(void)
{
int a[5] , i , j = 0;
for (i=0;i<=5;i++)
{
a[i] = j;
printf("%d " , a[5]);
}
return 0;
}
Please explain to me why the loop was infinite in first and not in second case.