Basic and Advance C Question:
Download Job Interview Questions and Answers PDF
Why is this loop always executing once?
Answer:
Why is this loop always executing once?
for(i = start; i < end; i++);
{
printf("%dn", i);
}
The accidental extra semicolon hiding at the end of the line containing the for constitutes a null statement which is, as far as the compiler is concerned, the loop body. The following brace-enclosed block, which you thought (and the indentation suggests) was a loop body, is actually the next statement, and it is traversed exactly once, regardless of the number of loop iterations.
for(i = start; i < end; i++);
{
printf("%dn", i);
}
The accidental extra semicolon hiding at the end of the line containing the for constitutes a null statement which is, as far as the compiler is concerned, the loop body. The following brace-enclosed block, which you thought (and the indentation suggests) was a loop body, is actually the next statement, and it is traversed exactly once, regardless of the number of loop iterations.
Download C Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
I am getting baffling syntax errors which make no sense at all | How can I call a function with an argument list built up at run time? |