• retagged by
1,927 views
1 1 vote
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;
}

2 Answers

3 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 1 vote

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

Position:
Show:

Related questions

9 9 votes
3 3 answers
436
436 views
GO Classes asked Jun 17
436 views
What is the output of the following code?#include <stdio.h void update(int n, int *p) { if (n <= 0) return; *p = *p + n; update(n - 2, p); *p = *p + n; } int main() { int...
6 6 votes
2 2 answers
368
368 views
GO Classes asked Jun 17
368 views
What is the output of the following code?#include <stdio.h int fun(int n) { int x = n; if (n <= 0) return 0; x = x + 2; return x + fun(n - 2); } int main() { printf("%d",...
6 6 votes
2 2 answers
408
408 views
GO Classes asked Jun 17
408 views
How many times is $\texttt{fun()}$ called when the following code is executed? (Count the first call also).#include <stdio.h int fun(int n) { if (n <= 1) return 1; return...
6 6 votes
3 3 answers
376
376 views
GO Classes asked Jun 17
376 views
What is the output of the following code?#include <stdio.h int fun(int n) { if (n <= 1) return n + 1; if (n % 2 == 0) return fun(n - 1) + fun(n - 2); return fun(n - 2) + ...