1,629 views
2 votes
2 votes

Does C perform array out of bound checking? What is the output of the following program?

int main()
{
    int i;
    int arr[5] = {0};
    for (i = 0; i <= 5; i++)
        printf("%d ", arr[i]);
    return 0;
}

4 Answers

1 votes
1 votes
Output would be 0 followed by 4 garbage values

we cannot say whether c provide bound checking or not infact  C don’t provide any specification which deal with problem of accessing invalid index. As per ISO C standard it is called undefined behaviour.
0 votes
0 votes
Ans 0,0,0,0,0

If we will not initialization then by default it contain 0 in each array element.
0 votes
0 votes
when executed on compiler this is the output : 0 0 0 0 0 32764 

this implies that c does not checks for outside bounds.

0 votes
0 votes
I think when you do arr[5]={0};

Then it initializing arr[0]=0;

Now the rest element of array is intialised to zero, now for the bound of the array we now that the array is continuous memmory location that why it will keep on printing the next memory loction as you was doing after five iteration and the value of that memory called  garbage (not needed for us),so i think c programming does perform array bound checking

Related questions

2 votes
2 votes
1 answer
1
5 votes
5 votes
1 answer
2
Markzuck asked Dec 22, 2018
1,196 views
someone please explain this:how does a+1 differs from &a+1 in above code?detailed explanation would be of great help as they incremented &a by 6 and NOT 1
2 votes
2 votes
2 answers
3