How does this “hello world!” program work?

后端 未结 2 901
滥情空心
滥情空心 2021-02-01 18:36
int main(void)
{ 
    return(\'yes\', *\"no\", **main, *********printf) (\"hello world!\\n\") *0; 
}

outputs hello world!, but how does it

2条回答
  •  醉梦人生
    2021-02-01 18:55

    Two things really:

    1. Function pointers don't dereference the same as other pointers. *main == main
    2. A comma separated list returns the value of the last element in the list

    So if we simplify the pointers:

    int main(void)
    { 
        return('yes', *"no", main, printf) ("hello world!\n") *0; 
    }
    

    And using the last element in the list as the value of the list

    int main(void)
    { 
        return printf("hello world!\n") *0; 
    }
    

    printf returns the number of characters printed

    int main(void)
    { 
        return 13 *0; 
    }
    

    And 13*0 is left as an exercise to the reader.

提交回复
热议问题