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

后端 未结 30 2930
旧时难觅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:01

    Buckle your seatbelts:

    #include 
    #include 
    
    int main()
    {
        jmp_buf env;
    
        if (!setjmp(env))
        {
            printf("if executed\n");
            longjmp(env, 1);
        }
        else
        {
            printf("else executed\n");
        }
    
        return 0;
    }
    

    Prints:

    if executed
    else executed
    

    Is this what you mean? I doubt it, but at least it's possible. Using fork you can do it also, but the branches will run in different processes.

提交回复
热议问题