787 views

No. of times '*' will be printed by the following C code is _____

#include<stdio.h>
void foo(int x)
{
switch(x){
case 1: printf("*");
case 2: printf("*");
case 3: printf("*");
default: printf("*");
}
}
int main()
{
foo(2.5);
}

foo(2.5);
foo(2) // converting 2.5 into int

now case 2 is true so it will print *.
case 3 will also print * because no break statement.
default case will also print *.

TOtal 3 time * will be printed.


@arjun sir, What if we have 2.99, it is still converting to 2, not 3.
2.99 stored in integer variable. so it will trancate part after decimal point i.e. if .99999 will be given it will take it as 2 only.
by default, when fractional(double) value are converted to int they are implicitly converted using floor(n) function..so no matter if you write 2.001 or 2.999 both will be converted to 2
......
x=2.5 after converting int x will 2

so * is prited 3 times (one for case 2 ,one for case 3 because no break statement is used here and one for default).

1
532 views
1 vote