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; } Programming in C programming-in-c output + – Nitesh Choudhary asked Jun 6, 2017 • retagged Mar 15, 2019 by Naveen Kumar 3 Nitesh Choudhary 902 views answer comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments Hira Thakur commented Jun 19, 2017 reply Follow Share what is the role of this code: printf("%d ",arr[i*10]); what is the meaning of arr[i*10]??? 0 votes 0 votes Jason GATE commented Jun 20, 2017 reply Follow Share printf("%d ",arr[i*10]); Here in arr[i*10] means address: arr[0] + 10 units for i=1; arr[i*10] means address: arr[0] + 20 units for i=2; and so on. Note : That simply shows array index out of bound.... That's the reason why this lines are compiler dependent and shows undefined behavior. Because some compilers shows it as Garbage Values and the Rest gives 0. 0 votes 0 votes turendar commented Aug 17, 2018 reply Follow Share Because syntax of given program code is true, But there is problem with array bound that is for i=2 arr[i * 10] is arr[20] Size of arr is 5 so it can be perform operation in the range arr[0] to arr[5] element arr[20] is not bound that is this element is out of bound hence it gives the garbage value 0 votes 0 votes Please log in or register to add a comment.
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. Rupendra Choudhary answered Jun 6, 2017 Rupendra Choudhary comment Share Follow See 1 comment See all 1 1 comment reply Nitesh Choudhary commented Jun 6, 2017 reply Follow Share Thanks 0 votes 0 votes Please log in or register to add a comment.
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 Jason GATE answered Jun 19, 2017 Jason GATE comment Share Follow See all 0 reply Please log in or register to add a comment.