On Sun, 18 Apr 2021 06:35:22 +0200
"Christian Jullien" <[email protected]> wrote:

> As Vincent said,
> main is a special function known by the C compiler which requires no 
> declaration.
> It accepts two different definitions (with implicit declaration):
> - one without arg
> - the other with int and char*[]
> 
> C verifies that definition matches the declaration. Correctly defined with 
> void (the prototype), a *definition* is perfectly legal without argument:
> 
> int foo(void);
> 
> int foo() {
>         return 42;
> }
> 
> gcc -std=c11 -Wall -c foo.c
> 
> 
> The same is true with g++
> g++ -std=c++17 -Wall -c foo.cpp
> 

As I said, I was reading C's standard and shared the URL; I am not talking 
about C++ nor comparing C to it.

My question was about main() and not any function's behavior.

> So it does not make difference if main is defined with or without (void)
>

Allow me to disagree here.

A simple example taken from 
https://www.geeksforgeeks.org/difference-int-main-int-mainvoid/ shows the 
difference:

    /* Without void in main() */

    int printf(const char*, ...);

    int main()
    {
        static int i = 5;
        if (--i) {
            printf("%d ", i);
            main(10);
        }
    }
 
$ gcc -Wall -Werror -std=c11 foo.c -o foo
$ ./foo
4 3 2 1
---------------------------------------------------------

    /* With void in main() */
        
    int printf(const char*, ...);

    int main(void)
    {
        static int i = 5;
        if (--i) {
            printf("%d ", i);
            main(10);
        }
    }

$ gcc -Wall -Werror -std=c11 foo.c -o foo
foo.c: In function ‘main’:
foo.c:8:9: error: too many arguments to function ‘main’
    8 |         main(10);
      |         ^~~~
foo.c:3:5: note: declared here
    3 | int main(void)
      |     ^~~~

> C.

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to