[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] Clarification about log message in commit 48df89e10e
From: |
Stefanos |
Subject: |
Re: [Tinycc-devel] Clarification about log message in commit 48df89e10e |
Date: |
Sun, 18 Apr 2021 13:01:05 +0300 |
On Sun, 18 Apr 2021 06:35:22 +0200
"Christian Jullien" <eligis@orange.fr> 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.