Ever look at old code and think? What the hell was I smoking?

Pages: 12345
> This is the "implementation defined" clause of the C standard -
> unfortunately C++ completely forbids it ...

This is what the C++ standard specifies:
It is implementation-defined whether a program in a freestanding environment is required to define a main function. [Note: In a freestanding environment, start-up and termination is implementation-defined; start-
up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]
In the C89 ANSI standard, it doesn't mention return type at all. It just says "The function called at program startup is named main"


That's odd, the C89 draft says:

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[]) { /*...*/ }

http://port70.net/~nsz/c/c89/c89-draft.html

But, I'm sure they got rid of all that cruft for the actual standard.


@JLBorges: ah, so does that mean that in a freestanding environment we're not even considering the main function as defined by the standard? That makes sense.
cire, excuse me for not adding the irrelevant information...?
NoXzema wrote:
cire, excuse me for not adding the irrelevant information...?


How is the part of the standard which specifies the type returned by main irrelevant to:
NoXzema wrote:
In the C89 ANSI standard, it doesn't mention return type at all.

I guess we're attracting more trolls. Just what we needed.
NoXzema wrote:
cire, excuse me for not adding the irrelevant information...?

Me and cire don't agree on much (almost nothing), but no part of the standard is irrelevant. I had wrongly assumed void main was part of the standard because I had a bad book to learn from that said it was. You can argue with cire on it, but when the creator of C++ (who obviously knows both C and C++) says void main was never part of either standard then it was never part of either standard.
It doesn't mention return type at all... what are you talking about?

It says that it needs to be main and describes what the arguments can be but does not mention return type.

EDIT: void main isn't part of the standard, but it's not illegal, contrary to popular belief.
Last edited on
1
2
int main(void);
int main(int argc, char *argv[]);

That isn't a definition, it's just an example of what it can be. Does it say it has to be int? Where does it say this?
closed account (N36fSL3A)
I think he was pointing out that the standard's only examples showed an integer type.
Last edited on
It doesn't really matters what the examples do. I should not be considered a troll whenever I rebuke a claim that is clearly wrong when you're pulling for straws by looking at examples rather than wording.

I've left this forum before over such a matter, I find it ridiculous that it's so blatant in this occasion again.
Those are not examples of how main can be declared, those are the two only valid forms main may be declared by.
closed account (N36fSL3A)
I'm with you NoXzema. (BTW, what was your last account name anyway?)

BHXSpecter wrote:
Me and cire don't agree on much (almost nothing), but no part of the standard is irrelevant. I had wrongly assumed void main was part of the standard because I had a bad book to learn from that said it was. You can argue with cire on it, but when the creator of C++ (who obviously knows both C and C++) says void main was never part of either standard then it was never part of either standard.
And guess what? You're blindly assuming that the people on the forums are correct as well. And also, not part of standard != illegal.

My microcontroller requires me to use void main(). I know they add a bit onto C to make it more uC friendly, but I doubt the changed that.
Last edited on
I believe in c89 the return type can be omitted as int is default.
http://ideone.com/GT86BO
http://ideone.com/shXdDC notice the runtime error
Those are not examples of how main can be declared, those are the two only valid forms main may be declared by.


Where does it say this? That's not how the standard works.. you cannot just declare rules to suit your own argument. It never says that these are the only valid forms of main.

EDIT: It does say that main may have either no parameters or two parameters if that's what you mean. It doesn't even mention the type of those two parameters and thus, any type can be used. Of course, doing so isn't reliable. http://ideone.com/J2bUu9
Last edited on
@Fredbill
It has already been noted that in stand alone programs the entry point is implementation defined, that's why you can use void main.
Fredbill wrote:
You're blindly assuming that the people on the forums are correct as well. And also, not part of standard != illegal.

No, I'm blindly following the link cire posted from Bjarne's personal page where he said void main was never part of the standard for C or C++. I believe Bjarne Stroustrup's word over anyone else seeing as he is the creator and would know the standard for his language. If it doesn't follow the standard then it is considered illegal to the standard. That is why any compiler that conforms to the standard will return an error when you try to use void main(). For example, GNU Compiler Collection returns
C

gcc -Wall -c "mainT.c" 
mainT.c:1:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main()
      ^
Compilation finished successfully.

C++

g++ -g -Wall -c "mainT.cpp"
mainT.cpp:1:11: error: ‘::main’ must return ‘int’
 void main()
           ^
Compilation failed.

Notice how the C variant didn't return an error and you had to turn on -Wall (this actually depends on how you compiled GCC apparently).
Last edited on
Yeah because I don't know of any programmer except beginners and those who don't care or are full of themselves that don't turn on -Wall and a few other flags as turning them on makes the compiler conform to the standard more. So it appears your argument only holds true by not caring if you make your compiler stick to the standard.
1
2
3
void main()
{
}


ln -s main.cpp main.c
echo '------------------- hosted -------------------------------'
clang -std=c11 -Wall -Wextra -pedantic-errors -c main.c && echo '*** ok ***' || echo '*** error ***'
gcc-4.8 -std=c11 -Wall -Wextra -pedantic-errors -c main.c && echo '*** ok ***' || echo '*** error ***'
echo '------------------- freestanding --------------------------'
clang -std=c11 -Wall -Wextra -pedantic-errors -ffreestanding -c main.c && echo '*** ok ***' || echo '*** error ***'
gcc-4.8 -std=c11 -Wall -Wextra -pedantic-errors -ffreestanding -c main.c && echo '*** ok ***' || echo '*** error ***'
------------------- hosted -------------------------------
main.c:1:1: error: 'main' must return 'int'
void main()
^~~~
int
1 error generated.
*** error ***
main.c:1:6: error: return type of 'main' is not 'int' [-Wmain]
 void main()
      ^
*** error ***
------------------- freestanding --------------------------
*** ok ***
*** ok ***

http://coliru.stacked-crooked.com/a/d0f571d306f00a2a
Pages: 12345