in Programming in C
1,167 views
3 votes
3 votes

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);
}
in Programming in C
by
1.2k views

2 Answers

5 votes
5 votes
Best answer
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.
selected by

4 Comments

@arjun sir, What if we have 2.99, it is still converting to 2, not 3.
1
1
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.
0
0
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
4
4
......
0
0
0 votes
0 votes
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).
Answer:

Related questions