GATE 2015 CS (CS3) – Question 53
How many times is printf executed?
#include <stdio.h>
int main(void) {
int i,j,k=0;
j=2*3/4+2.0/5+8/5;
k-=--j;
for(i=0;i<5;i++) {
switch(i+k) {
case 1:
case 2: printf("\n%d",i+k);
case 3: printf("\n%d",i+k);
default: printf("\n%d",i+k);
}
}
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 10
Explanation
Integer subexpressions give 2*3/4=1 and 8/5=1; with 2.0/5=0.4 the sum is 2.4, converted to j=2. Predecrement gives j=1 and k=−1. Switch arguments are −1,0,1,2,3. With no breaks, they execute one,one,three,three,two prints respectively, totalling ten.