I got the first problem in an interview question.But I want a proper explanation for the question.I try this in home and some other confusions are also rises.
#include <stdio.h>
int main()
{
int arr[4]={10,20,30,40};
int i;
for(i=0;i<=4;i++)
printf("%d,",arr[i]);
printf("\n");
return 0;
}
OUTPUT
10,20,30,40,4,
The last out put was 4.but it is out of array index.Again I think that in memory variable i present after array elements.So I get this answer.
But again i confuse with this
#include <stdio.h>
int main()
{
char arr[4]={10,20,30,40};
int i;
for(i=0;i<=4;i++)
printf("%d,",arr[i]);
printf("\n");
return 0;
}
OUTPUT
10,20,30,40,0,
Again more confuse with below
#include <stdio.h>
int main()
{
int arr[4]={10,20,30,40};
char i;
for(i=0;i<=4;i++)
printf("%d,",arr[i]);
printf("\n");
return 0;
}
OUTPUT
10,20,30,40,74743796,
Can any body Explain Why this type of variation in output?
I use intel cpu,Ubuntu os,Gcc complier..
If compiler specific or architecture specific then also mention in the answer please.