-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.
    – binarym
    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)
    – Edd Inglis
    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.

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
  • Why does the program print "HelloWorldThere" when we add semi-colon? Commented Jan 15, 2020 at 6:59
  • @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. Commented Jan 15, 2020 at 10:54
  • Oh! Now I get it. Thanks for the reply ! Commented Jan 15, 2020 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.