Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #147462 > unrolled thread

Why does 'gcc -Wall' complain about void main() ?

Started byDFS <nospam@dfs.com>
First post2019-11-01 10:22 -0400
Last post2019-11-01 10:25 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  Why does 'gcc -Wall' complain about void main() ? DFS <nospam@dfs.com> - 2019-11-01 10:22 -0400
    Re: Why does 'gcc -Wall' complain about void main() ? David Brown <david.brown@hesbynett.no> - 2019-11-01 15:45 +0100
      Re: Why does 'gcc -Wall' complain about void main() ? Manfred <noname@add.invalid> - 2019-11-01 17:57 +0100
        Re: Why does 'gcc -Wall' complain about void main() ? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:31 -0700
          Re: Why does 'gcc -Wall' complain about void main() ? Manfred <noname@add.invalid> - 2019-11-01 21:00 +0100
        Re: Why does 'gcc -Wall' complain about void main() ? David Brown <david.brown@hesbynett.no> - 2019-11-01 21:20 +0100
    Re: Why does 'gcc -Wall' complain about void main() ? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:25 -0700

#147462 — Why does 'gcc -Wall' complain about void main() ?

FromDFS <nospam@dfs.com>
Date2019-11-01 10:22 -0400
SubjectWhy does 'gcc -Wall' complain about void main() ?
Message-ID<5fXuF.717$gt2.335@fx02.iad>
gcc 9.1.0 on MSYS2

combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    14 | void main() {
       |      ^~~~





Aside: gcc is more colorful and informative than I remember

notes in light blue
errors in red
warnings in purple
data types in green

https://imgur.com/a/xcbp2Lg

[toc] | [next] | [standalone]


#147468

FromDavid Brown <david.brown@hesbynett.no>
Date2019-11-01 15:45 +0100
Message-ID<qphgf3$gqr$2@dont-email.me>
In reply to#147462
On 01/11/2019 15:22, DFS wrote:
> gcc 9.1.0 on MSYS2
> 
> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
>    14 | void main() {
>       |      ^~~~
> 

That is because the main should be declared as either:

	int main(void);

	int main(int argc, char *argv[]);

or an implementation-specific extension (*nix systems often allow a
third "envp" pointer).

Compilers are often quite lax about warning about non-standard "main"
signatures in their default modes, but more enthusiastic if you enable
lots of warnings.

For future reference - gcc has documentation on all its warnings in its
reference manual.  Since the compiler helpfully tells you exactly which
warning triggered the message, it is extremely easy to look it up:

<https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html>

> 
> 
> Aside: gcc is more colorful and informative than I remember
> 


That is because it has been adding colour and more helpful indicators of
exactly what is wrong in your code (and even how to fix it) with each
new version of the compiler.



> notes in light blue
> errors in red
> warnings in purple
> data types in green
> 
> https://imgur.com/a/xcbp2Lg

[toc] | [prev] | [next] | [standalone]


#147470

FromManfred <noname@add.invalid>
Date2019-11-01 17:57 +0100
Message-ID<qpho6i$9bj$1@gioia.aioe.org>
In reply to#147468
On 11/1/2019 3:45 PM, David Brown wrote:
> On 01/11/2019 15:22, DFS wrote:
>> gcc 9.1.0 on MSYS2
>>
>> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
>>     14 | void main() {
>>        |      ^~~~
>>
> 
> That is because the main should be declared as either:
> 
> 	int main(void);
> 
> 	int main(int argc, char *argv[]);
> 
> or an implementation-specific extension (*nix systems often allow a
> third "envp" pointer).

And version 0.01 of the linux kernel had:

void main(void)      /* This really IS void, no error here. */
{                    /* The startup routine assumes (well, ...) this */

[toc] | [prev] | [next] | [standalone]


#147476

FromKeith Thompson <kst-u@mib.org>
Date2019-11-01 10:31 -0700
Message-ID<lnftj77bc3.fsf@kst-u.example.com>
In reply to#147470
Manfred <noname@add.invalid> writes:
> On 11/1/2019 3:45 PM, David Brown wrote:
>> On 01/11/2019 15:22, DFS wrote:
>>> gcc 9.1.0 on MSYS2
>>>
>>> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
>>>     14 | void main() {
>>>        |      ^~~~
>>>
>> 
>> That is because the main should be declared as either:
>> 
>> 	int main(void);
>> 
>> 	int main(int argc, char *argv[]);
>> 
>> or an implementation-specific extension (*nix systems often allow a
>> third "envp" pointer).
>
> And version 0.01 of the linux kernel had:
>
> void main(void)      /* This really IS void, no error here. */
> {                    /* The startup routine assumes (well, ...) this */

The kernel is considered to be in a freestanding environment, in
which the program's entry point is entirely implementation-defined.
For hosted environments, main returns int (or possibly some
implementation-defined type, but that's rarely if ever useful).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#147486

FromManfred <noname@add.invalid>
Date2019-11-01 21:00 +0100
Message-ID<qpi2sl$1qeu$1@gioia.aioe.org>
In reply to#147476
On 11/1/2019 6:31 PM, Keith Thompson wrote:
> Manfred <noname@add.invalid> writes:
>> On 11/1/2019 3:45 PM, David Brown wrote:
>>> On 01/11/2019 15:22, DFS wrote:
>>>> gcc 9.1.0 on MSYS2
>>>>
>>>> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
>>>>      14 | void main() {
>>>>         |      ^~~~
>>>>
>>>
>>> That is because the main should be declared as either:
>>>
>>> 	int main(void);
>>>
>>> 	int main(int argc, char *argv[]);
>>>
>>> or an implementation-specific extension (*nix systems often allow a
>>> third "envp" pointer).
>>
>> And version 0.01 of the linux kernel had:
>>
>> void main(void)      /* This really IS void, no error here. */
>> {                    /* The startup routine assumes (well, ...) this */
> 
> The kernel is considered to be in a freestanding environment, in
> which the program's entry point is entirely implementation-defined.
> For hosted environments, main returns int (or possibly some
> implementation-defined type, but that's rarely if ever useful).
> 

Indeed, and in fact main() has been renamed into some other init 
function as entry point of the kernel since long by now (I think it's 
start_kernel, although the source is still called main.c).
But I found the comment by Linus kind of interesting (possibly a warning 
for the unwary..)

[toc] | [prev] | [next] | [standalone]


#147488

FromDavid Brown <david.brown@hesbynett.no>
Date2019-11-01 21:20 +0100
Message-ID<qpi41n$giv$1@dont-email.me>
In reply to#147470
On 01/11/2019 17:57, Manfred wrote:
> On 11/1/2019 3:45 PM, David Brown wrote:
>> On 01/11/2019 15:22, DFS wrote:
>>> gcc 9.1.0 on MSYS2
>>>
>>> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ 
>>> [-Wmain]
>>>     14 | void main() {
>>>        |      ^~~~
>>>
>>
>> That is because the main should be declared as either:
>>
>>     int main(void);
>>
>>     int main(int argc, char *argv[]);
>>
>> or an implementation-specific extension (*nix systems often allow a
>> third "envp" pointer).
> 
> And version 0.01 of the linux kernel had:
> 
> void main(void)      /* This really IS void, no error here. */
> {                    /* The startup routine assumes (well, ...) this */
> 
> 

That's fine, because the OS is a freestanding C program, not a hosted C 
program.  It can use whatever form of "main" the compiler supports - a 
return type of "void" is perfectly reasonable for a program that will 
never return to the controlling OS, because it /is/ the controlling OS.

[toc] | [prev] | [next] | [standalone]


#147475

FromKeith Thompson <kst-u@mib.org>
Date2019-11-01 10:25 -0700
Message-ID<lnk18j7blv.fsf@kst-u.example.com>
In reply to#147462
DFS <nospam@dfs.com> writes:
> gcc 9.1.0 on MSYS2
>
> combinearrays.c:14:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
>     14 | void main() {
>        |      ^~~~

This newsgroup has an FAQ at <http://www.c-faq.com>.
Section 11 answers your question.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web