1

I am preparing for my school test. So for that I have visited lots of websites to clear my doubts about C, C++, Java programming languages. While I was reading those questions, I got one MCQ question which is below:

switch (printf("Do"))
{
    case 1:
        //code here
    case 2:
        //code here    
}

I execute this code and got execution of case 2 but I'm not getting why it is executed. Can any one help me to understand?

P.W
  • 26,289
  • 6
  • 39
  • 76

1 Answers1

3

printf returns the number of characters it has printed if successful. So in your case it returns 2 and therefore the code under case 2: is executed.

P.W
  • 26,289
  • 6
  • 39
  • 76