≡ Menu

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

{ 1 comment… add one }
  • Amrendra Lal July 22, 2011, 5:04 pm

    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 Comment