retagged by
890 views
1 votes
1 votes
What is the o/p of following code?

#include  <stdio.h>

int main(void)
{
    int arr[5]={1,2,3,4,5};
    int i;
    for(i=2;i<=5;i++)
    printf("%d ",arr[i*10]);
    return 0;
}
retagged by

2 Answers

3 votes
3 votes

o/p : Undefined Behavior i mean some random values (garbage)

Actually it won't generate segmentation fault because C don't check array bounds to make their work fast.

Bound checking would make the execution slower think one time like every time you access an element of array , compiler performs whether this element is out of bound or not . C don't do this for optimization purpose.

1 votes
1 votes

I think this Program is Compiler Dependent.

1:Codechef Online Compiler : Prints Garbage Values.

2. TutorialS Point Online Compiler : Prints all Zeroes. 0 0 0 0

Related questions