-3

Can someone explain why the code prints "HelloWorld" and not "HelloWorldThere" ? Also why does it print anything at all as there are no conditions in if or switch statements ? Here is the code:

#include <stdio.h> int main() { int a, b; if(printf("Hello")) switch(printf("World")) while(printf("There")) { return 0; } } 
7
  • 3
    You might want to read about what printf returns. Commented Jan 14, 2020 at 17:02
  • 1
    And about switch statement too ... Your code is totally a non-sense. Commented Jan 14, 2020 at 17:05
  • 3
    About switch: "If expression evaluates to a value that doesn't match any of the case: labels, and the default: label is not present, none of the switch body is executed." -- no case labels, so nothing to match the expression, and no default label, so nothing happens. Commented Jan 14, 2020 at 17:07
  • I've been writing C professionally for nearly 20 years and never knew this: stackoverflow.com/questions/29023601/…. (Edited to provide a better link) Commented Jan 14, 2020 at 17:09
  • 1
    @EdHeal: Yes, it compiles and runs, and produces the output described by the OP. Commented Jan 14, 2020 at 17:13

2 Answers 2

7

Pretty straightforward: printf("Hello") returns 5 (the number of characters written). 5 is not 0, so it's considered "true" for the purposes of the if, so then printf("World") also returns 5, the switch looks for a case 5:, doesn't find one, and stops there.

Sign up to request clarification or add additional context in comments.

Comments

5

For starters, let's consider what the function printf returns. From the C Standard

3 The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

So the condition of this if statement

if(printf("Hello")) 

evaluates to true because printf() returns a non-zero value.

Then this switch statement

switch(printf("World")) 

is evaluated.

Now let's consider how the switch statement works. From the C Standard

4 A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.

As the body statement of the switch statement does not have a label (including the default label) then the control is passed past the body statement. That is, the while statement (that is a body statement of the switch statement) is not executed.

If you want to get the expected by you result then for example insert the label default.

#include <stdio.h> int main() { if(printf("Hello")) switch(printf("World")) default: while(printf("There")) { return 0; } } 

In this case the program output is

HelloWorldThere 

Or use a null-statement as the body statement of the switch statement.

#include <stdio.h> int main() { if(printf("Hello")) switch(printf("World")); // <== while(printf("There")) { return 0; } } 

3 Comments

Why does the program print "HelloWorldThere" when we add semi-colon?
@ChinmayVemuri Now the body statement of the switch statement is a nul statement. So the while statement is a separate statement that is executed after the switch statement.
Oh! Now I get it. Thanks for the reply !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.