Can the C main() function be static?

前端 未结 5 815
北荒
北荒 2021-01-01 17:00

Can the main() function be declared static in a C program? If so then what is the use of it?

Is it possible if I use assembly code and call

5条回答
  •  旧巷少年郎
    2021-01-01 17:11

    You could have a static function called main() in a source file, and it would probably compile, but it would not be the main() function because it would be invisible to the linker when the start-up code (crt0.o on many (older) Unix systems) calls main().

    Given the code:

    static int main(int argc, char **argv)
    {
        return(argv + argc);
    }
    
    extern int x(int argc, char **argv)
    {
        return(main(argc, argv));
    }
    

    GCC with -Wall helpfully says:

     warning: 'main' is normally a non-static function
    

    Yes, it can be done. No, it is normally a mistake - and it is not the main() function.

提交回复
热议问题