Why warning main return type is not int?

Go To Last Post
29 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, why this warning? I know at Os systems, main should return exit code to OS, but why on MCU?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because you are using GCC and it's really a compiler for x86 running "grown up" operating systems and it insists the prototype for main is "int main(void)". Other C compiler for AVR allow for the more obvious "void main(void)"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

С99 (ISO/IEC 9899:1999) says:

Quote:
5.1.2.2.1 Program startup
1
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.

So, gcc just conforms standard.

Add -wno-main switch to disable warning.

wbr, ReAl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

C89 was more friendly to us :-)

Quote:
2.1.2 Execution environments

Two execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function
is called by the execution environment. All objects in static storage shall be initialized (set to their initial values) before program
startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution
environment.

2.1.2.1 Freestanding environment
In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and
type of the function called at program startup are implementation-defined. There are otherwise no reserved external identifiers. Any library facilities available to a freestanding program are implementation-defined.
The effect of program termination in a freestanding environment is implementation-defined.

2.1.2.2 Hosted environment
A hosted environment need not be provided, but shall conform to the following specifications if present.

"Program startup"
The function called at program startup is named main. The implementation declares no prototype for this function. It can be
defined with no parameters:
int main(void) { /*...*/ }
or with two parameters (referred to here as argc and argv , though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /*...*/ }

wbr, ReAl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

So, gcc just conforms standard.

Quote:

C89 was more friendly to us

C99 draft that I use has the same wording about freestanding environment. From your post(s) you imply that official C99 does >>not<< have the same mention of freestanding?!?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

C99 still has the same clause WRT freestanding environments. [5.1.2.1] So GCC could be updated to allow for this, I'm just guessing that nobody has the time, or desire, to fix this minor annoyance.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At least avr-gcc issues a warning rather than an error.

Other compilers give an error if you use int main() in an embedded system.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oh, I am sorry my sleepy eyes
Yes, C99 has the same text "In a freestanding environment ... the name and type of the function called at program startup are implementation-defined"

wbr, ReAl

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
At least avr-gcc issues a warning rather than an error.

Other compilers give an error if you use int main() in an embedded system.

David.

That's not necessarily a fault. As the compiler is targeted at a freestanding environment, the entry function can be whatever they deem appropriate, it need not even be called "main". And as it is for a freestanding environment, they do not need to support the forms of main that are designed to (accept data from and/or) return to the OS.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

C99 still has the same clause WRT freestanding environments. [5.1.2.1] So GCC could be updated to allow for this, I'm just guessing that nobody has the time, or desire, to fix this minor annoyance.


Quote:

At least avr-gcc issues a warning rather than an error.

Other compilers give an error if you use int main() in an embedded system.

David.


How soon we forget, eh? It was not that long ago when there was no catch loop, in order to make main() re-entrant...
https://www.avrfreaks.net/index.p...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

not sure how that was to relate to my comment, exept perhaps to point out that things do get fixed... indeed they do, and the catch loop is a little different as it relies on the init code, not intrinsic behaviour of the compiler. Having said that, reading through the thread reveals that there is a compiler switch to indicate a freestanding environment, but for some explicable readon it also affects optimization, making it useless. [I have no idea why such a link would exist, it makes no sense to me as tyo why the compiler cannot make teh same assumptions for optimization for freestanding environment than it can in a hosted one]

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pokevitek, you can declare

void main(void)

don't return an int and you will get no error.
This seems a little obvious so maybe I am missing a deeper point.
JS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
This seems a little obvious so maybe I am missing a deeper point.
The OP didn't say he got an error, he got a warning, and it is the very signature that you posted that caused the warning. If you use:

int main(void)

you don't get the warning.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, so C was designed mainly for OS-controlled x86 CPU? I thought that C was designed mainly as substitute for assembler, platform independent.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I thought that C was designed mainly as substitute for assembler, platform independent.

Suggest you read the first few pages of K&R if you believe that.

This is a quite famous picture of the two of them operating the machine they developed C on:

Don't know about you but that doesn't look like a microcontroller to me :lol:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
This is a quite famous picture of the two of them operating the machine they developed C on
Just some computer-historical trivia, "they" is inappropriate. Brian Kernighan is best known for contribution to the *book*, but he did not co-develop the *language*.
Brian Kernighan in an interview wrote:
[...] C is entirely the work of Dennis Ritchie [...]

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That interesting point brought me to this intriguing article by Dennis Ricthie:

http://cm.bell-labs.com/cm/cs/wh...

I think anyone programming in C should read that to get a real picture of how C developed the way it did. As you'll read there the original work was on "B" which was developed on a machine with 8K words of 18bit memory. It was Ritchie who wrote the first machine instruction generator for it (previously it was threaded interpreted - presumably a bit like Forth?). It wasn't until they replaced the PDP-7 with a PDP-11 that had 24K of memory that C was developed with Unix occupying about 12K of the memory and leaving the rest for the C compiler and the programs it generated.

So, yes, even then C programs were returning to a Unix operating system (albeit only 12K) and presumably wanting to pass back an "ERRORLEVEL" to say whether they worked or not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I have read that article around a year ago. And, yes, I couldn't have even imagined to work in such circumstances. (Although I had some casual experience with mainframes and the punched cards culture, my first true computer experiences fell into the 8-bitters era (mind my location).) Memory and processing time limitations of that era are unimaginable today - and it's only a single generations' time, isn't it.

But what he wrote about the language itself... truly hardened me in my C hatred...

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

this intriguing article by Dennis Ricthie

"We all wanted to create interesting software more easily. Using assembler was dreary enough that..."

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

This is a quite famous picture of the two of them operating the machine they developed C on:

The guy standing is Dennis Ritchie. The seated fellow is not Kernighan though.

JS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The guy standing is Dennis Ritchie. The seated fellow is not Kernighan though.

Ah apologies. I'd always been led to believe that rather famous picture was the two of them.

In fact I got to that picture (one of the top hits) by simply typing "Kerninghan Ritchie" into Google's image search so I guess someone else must have thought the same as me too:

http://www.google.co.uk/images?r...

EDIT: I should have read the text surrounding the original location of that picture which tells us that the seated gentleman is the equally famous Ken Thompson (the original "Mr. B")

Last Edited: Tue. Aug 24, 2010 - 04:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jonsoons wrote:
clawson wrote:

This is a quite famous picture of the two of them operating the machine they developed C on:

The guy standing is Dennis Ritchie. The seated fellow is not Kernighan though.

JS

site I found through google for this picture wrote:
Figure 1: Dennis Ritchie, inventor of the C programming language stands next to Ken Thompson, original inventor of Unix, designing the original Unix operating system at Bell Labs on a PDP-11

Then *both* in Cliff's post above is quite appropriate: Thompson's work, according to that Ritchie article, formed a significant part of C.

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wek wrote:
Then *both* in Cliff's post above is quite appropriate: Thompson's work, according to that Ritchie article, formed a significant part of C.

JW

It is the antecedent of the "them" that is in doubt. But that has been cleared up.
JS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

freestanding vs. hosted:

ReAlAl wrote:
oh, I am sorry my sleepy eyes
Yes, C99 has the same text "In a freestanding environment ... the name and type of the function called at program startup are implementation-defined"

There's one issue that hasn't been mentioned: The definition of a freestanding environment. It seems to be accepted here that an AVR program is to be considered as running in a freestanding envirenment. This point is not so clear to me:

Quote:

A conforming
freestanding implementation shall accept any strictly conforming program that does not
use complex types and in which the use of the features specified in the library clause
(clause 7) is confined to the contents of the standard headers ,
,
, , , , and
.

Note the omission of several headers (e.g. string.h) which excludes the use of important parts of the standard library. So I don't think the typical AVR program qualifies as running in a freestanding environment.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Before anyone considers the use of -ffreestanding they should read this:

https://www.avrfreaks.net/index.p...

also the details of -ffreestanding here:

http://gcc.gnu.org/onlinedocs/gc...
which leads to
http://gcc.gnu.org/onlinedocs/gc...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> So I don't think the typical AVR program qualifies as
> running in a freestanding environment.

That's exactly the point. The typical AVR program doesn't *want* to
be "freestanding" in the standard's sense, as it would lose some
compiler optimizations. (Note that a freestanding implementation is,
of course, allowed to extend the set of header files it accepts. It's
just not required to do so.)

As it doesn't *want* to be fully freestanding, either live with the
warning, disable the warning selectively, or simply use the "int"
return type without questioning it further -- the compiler won't ask
you for an acutal "return" statement either when it sees that the
program eventually ends up in an infinite loop anyway.

Most people appear to simply pick option #3, as it simply doesn't
hurt doing so, and doesn't require further modifications.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just for completeness:

root@migrate:/var/www# vim test.c
root@migrate:/var/www# gcc -o test test.c
root@migrate:/var/www# ./test
Wot! No warning?root@migrate:/var/www# cat test.c
#include 
void main(void) { printf("Wot! No warning?"); }
root@migrate:/var/www# 

JS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
5.1.2.2.1 Program startup
1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.
I think that the last clause in that statement pretty much gives the implementer free reign to do anything they what outside of changing the name from 'main', even if not freestanding.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The last clause is certainly there because historically,
Unices commonly used another (possible) prototype:

int main(int argc, char **argv, char **environ);

Although Posix no longer mentions this form (instead, environ
is a global variable there), it was apparently reason enough for
the C standard to not completely forbid it. The C99 Rationale
says:

[...] While many
implementations support more than two arguments to main, such practice is neither blessed nor
forbidden by the Standard; a program that defines main with three arguments is not strictly
conforming (see §J.5.1.).

While it is true that other prototypes could be acceptable under this
rule as well, GCC simply follows Unix tradition here (which is not
surprising, given its Unix origin). It accepts the two prototypes
that are mandated by the standard, it does not complain about a third
char ** argument (the historical `environ'), and warns about
everything else.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.