Identify the error in the following C statement - counter
Sponsored Links


Popular Links


List of Abbreviations

National Highways in India

States & Union Territories of India

Currency of All Countries

Identify the error in the following C statement

You are Here: Home > Computer Subjects > Programming in C Language > …

Identify the error in the following C statement

 int x[8], i; for (i = 0; i < = 8; ++i) x[i] = i; 

Will the error be defected? If so when?

Ans. Error.

After the 8th iteration the following code will generate an error. This is because, the array x can store only 8 element which is the maximum size of it (in this example). But the for loop will execute for 9 times. Thus at the 9th iteration, code inside the loop will try to store the value of i into the 9th position of array x, which violates the array rule and will generate an error (Array index out of bounds error).

 

 Click Here to Find Latest Jobs and Current Affairs

Updated: May 31, 2013 — 2:04 pm

1 Comment

Add a Comment
  1. As above c code written, in this situation C compiler never shows any error message but it will forcely write and prints from 0 to 8, means total 9 values, even size of the array is 8.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2016. All Rights Reserved. WikiIND.com