Why a warning of “control reaches end of non-void function” for the main function?

后端 未结 3 953
野性不改
野性不改 2021-02-18 17:25

I run the following C codes and got a warning: control reaches end of non-void function

int main(void) {}

Any suggestions?

3条回答
  •  天涯浪人
    2021-02-18 18:02

    Just put return 0 in your main(). Your function main returns an int (int main(void)) therefore you should add a return in the end of it.

    Control reaches the end of a non-void function

    Problem: I received the following warning:
    

    warning: control reaches end of non-void function

    Solution: This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.

    source

    Solution:

    int main(void)
    {
        my_strcpy(strB, strA);
        puts(strB);
        return 0;
    }
    

提交回复
热议问题