-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;
            }
}
Chinmay Vemuri
  • 179
  • 2
  • 9
  • 3
    You might want to read about what `printf` [returns](https://en.cppreference.com/w/c/io/fprintf#Return_value). – Some programmer dude Jan 14 '20 at 17:02
  • 1
    And about `switch` statement too ... Your code is totally a non-sense. – binarym Jan 14 '20 at 17:05
  • 3
    About [`switch`](https://en.cppreference.com/w/c/language/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. – Fred Larson Jan 14 '20 at 17:07
  • I've been writing C professionally for nearly 20 years and never knew this: https://stackoverflow.com/questions/29023601/can-i-put-code-outside-of-cases-in-a-switch. (Edited to provide a better link) – Edd Inglis Jan 14 '20 at 17:09
  • Does this compile? Also use braces – Ed Heal Jan 14 '20 at 17:09
  • 1
    @EdHeal: [Yes, it compiles and runs](https://ideone.com/iB5NUz), and produces the output described by the OP. – Fred Larson Jan 14 '20 at 17:13
  • Does this answer your question? [Valid, but worthless syntax in switch-case?](https://stackoverflow.com/questions/41727415/valid-but-worthless-syntax-in-switch-case) – Yunnosch Jan 14 '20 at 17:19

2 Answers2

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.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
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;
            }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335