C void function with return value

前端 未结 2 1519
一整个雨季
一整个雨季 2020-12-19 10:23

As far as I know, return statement in void function will throw an error.

But in the below program that is not the case.

Here the displayed output

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 11:15

    The code you've presented is actually invalid in C99 for a variety of reasons, but the most painful one is below:

    foo.c:5:17: warning: implicit declaration of function 'fun' is invalid in C99 [-Wimplicit-function-declaration]
        printf("%d",fun(fun(fun(i))));
                    ^
    foo.c:8:6: error: conflicting types for 'fun'
    void fun(int i)
         ^
    foo.c:5:17: note: previous implicit declaration is here
        printf("%d",fun(fun(fun(i))));
                    ^
    

    Note that if you gave a function prototype for fun(), which you really should, then you would get a different set of errors:

    foo.c:7:25: error: passing 'void' to parameter of incompatible type 'int'
        printf("%d",fun(fun(fun(i))));
                            ^~~~~~
    

提交回复
热议问题