in Programming in C retagged by
3,246 views
4 votes
4 votes

Output of the following loop is

for(putchar('c');putchar
('a');putchar('r'))
putchar('t');
  1. a syntax error.
  2. cartrt.
  3. catrat.
  4. catratratratrat...

 

in Programming in C retagged by
by
3.2k views

1 Answer

5 votes
5 votes
Correct answer is D:

Explanation:

Control flow of for loop is like:

1. Initialize

2. Condition check

3. Execute loop body

4. Update

5. Repeate from 2 to 4 untill condition holds.

So this is how control flow of for loop is to be implemented.

Now, in given question:

for(putchar('c');putchar('a');putchar('r'))
        putchar('t');

1. Initializarion:  putchar('c') results: c

2. Condition check : putchar('a') results : a

3. Executions of loop body : putchar('t') results : t

4. Updation : putchar('r') results : r

5. Repition : atratratratratratr......

final result: catratratratratrat......

As we can see it is an infinte loop so the correct answer is: D

if it would have been implemented using Recursion, then this will be continued until the runtime stack get exhausted. But just because it has been implemented using for loop it will be continuing itself like this forever.
by
Answer:

Related questions