What's the “condition” in C interview test?

后端 未结 30 2721
旧时难觅i
旧时难觅i 2020-12-05 02:37

Would it be possible to print Hello twice using single condition?

if  \"condition\"
  printf (\"Hello\");
else
  printf(\"World\");         


        
相关标签:
30条回答
  • 2020-12-05 03:10

    Without knowing the return value of printf off the top of your head:

    if (printf("Hello") && 0)
        printf("Hello");
    else
        printf("World");
    
    0 讨论(0)
  • 2020-12-05 03:10

    Abuse of preprocessing - with cleanup at least.

    
    #define else 
    if(1)
    {
       printf("hello");
    }
    else
    {
       printf("world");
    }
    #undef else
    
    0 讨论(0)
  • 2020-12-05 03:11
    if ( printf("Hello")==0)
    

    see [http://www.coders2020.com/what-does-printf-return]

    (matt corrected my =, thanks, C is far away)

    0 讨论(0)
  • 2020-12-05 03:11
     #include<stdio.h>
      int main()
    {
     if(! printf("Hello"))
         printf ("Hello");
    else
        printf ("World");
     return 0;
    }
    

    Because Printf returns the number of character it has printed successfully.

    0 讨论(0)
  • 2020-12-05 03:14

    Just put the code before or after the if..else block.

    Alternatively, if you have an "if, else if, else" block where you want to execute code in some (but not all) branches, just put it in a separate function and call that function within each block.

    0 讨论(0)
  • 2020-12-05 03:15
    if ( printf("Hello") == 0 )
        printf ("Hello");
    else
        printf ("World");
    

    :-)

    0 讨论(0)
提交回复
热议问题