Skip to content
Related Articles

Related Articles

Improve Article

C Quiz – 104 | Question 2

  • Difficulty Level : Basic
  • Last Updated : 28 Jun, 2021
Geek Week

With respect to following “for” loops in C, pick the best statement. Assume that there is a prior declaration of ‘i’ in all cases

for (i = 0; i < 10 ; i++) // (i)
for ( ; i < 10 ; i++) // (ii)
for (i = 0;  ; i++) // (iii)
for (i = 0; i < 10 ; ) // (iv)
for ( ; ; ) // (v)

(A) Only (i) and (v) would compile successfully. Also (v) can be used as infinite loop.
(B) Only (i) would compile successfully.
(C) All would compile successfully but behavior of (ii), (iii) and (iv) would depend on compiler.
(D) All would compile successfully.

Answer: (D)

Explanation: In C, any of the 3 expressions of “for” loop can be empty. The exact behavior of the loop depends on the body of the loop as well. Basically, all of the 3 expressions of loop can be put inside the loop body. So as per C language standard, all of the above are valid for loops.

Quiz of this Question

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :