Legality of void main in C

  • Thread starter prasoonthegreat
  • Start date
P

prasoonthegreat

Is void main legal in C....I know according to the standards it is
illegal in C++....

But after reading the standards.....

The C Standard currently says that an implementation is free to define
any additional forms for main that it cares to. § 5.1.2.2.1.1 of the C
Standard lists the allowable forms for the definition of main. Because
of the semi-colon, its final sentence parses as follows:

It shall be defined

* with a return type of int and
o with no parameters [...] or
o with two parameters [...] or equivalent;
or
* in some other implementation-defined manner.

(Furthermore, § 5.1.2.2.3.1 says, of main,

If the return type is not compatible with int, the termination
status returned to the host environment is unspecified.

which indicates that allowing forms that do not return int is
intentional.) .......


So can it(void main) be considered legal in C or not.....It is known
to me that using void main is dangerous as it leads to process stack
corruption sometimes .....

So please focus on its legality!!!!!

Prasoon
 
K

Keith Thompson

Is void main legal in C....I know according to the standards it is
illegal in C++....

The C standard doesn't use the word "legal" or "illegal" in that
sense.
But after reading the standards.....

The C Standard currently says that an implementation is free to define
any additional forms for main that it cares to. § 5.1.2.2.1.1 of the C
Standard lists the allowable forms for the definition of main. Because
of the semi-colon, its final sentence parses as follows:

It shall be defined

* with a return type of int and
o with no parameters [...] or
o with two parameters [...] or equivalent;
or
* in some other implementation-defined manner.

(Furthermore, § 5.1.2.2.3.1 says, of main,

If the return type is not compatible with int, the termination
status returned to the host environment is unspecified.

which indicates that allowing forms that do not return int is
intentional.) .......

Yes, an implementation is permitted to allow void main. A program
that uses void main is valid for such an implementation; for other
implementations, such a program's behavior is undefined, and the
compiler is free to reject it outright.
So can it(void main) be considered legal in C or not.....It is known
to me that using void main is dangerous as it leads to process stack
corruption sometimes .....

So please focus on its legality!!!!!

You might as well assume that void main is "illegal"; there's no good
reason to use it (except perhaps in a freestanding implementation).
 
A

arnuld

The C standard doesn't use the word "legal" or "illegal" in that
sense.


Just want to know, if someone uses void main() what word I should use (if
illegal is not the one).
 
J

James Kuyper

arnuld said:
Just want to know, if someone uses void main() what word I should use (if
illegal is not the one).

In general, there's no single word that can be substituted for
"illegal". You generally need multiple words, and you need different
words in different contexts, and you might need to reorganize other
parts of your sentence to make those words fit in. In most cases where
you would want to say (incorrectly) that a piece of C codes "is
illegal", the more accurate wording is one or more of the following:

"is a constraint violation"
"is a syntax error"
"exceeds a minimum implementation limit"
"has undefined behavior"

In some cases, whether or not one of the above phrases applies depends
upon unspecified behavior, in which case you need to prefix your
statement with something like "it is implementation-specific whether
....". If the implementation is required to document which choice it
makes for unspecified behavior, you can replace "specific" with "defined".

In this particular case, you would say "It is implementation-defined
whether or not void main() is supported. If it is not, use of void
main() has undefined behavior".

A more general statement, that covers all of the above, is
"non-portable". A fully conforming compiler is free to define behavior
that the C standard leaves undefined, to set its own implementation
limits higher than the minimum levels, and to accept code that contains
syntax errors or constraint violations. Therefore, whether or not a
given piece of code will cause you trouble always depends upon which
conforming compiler you use.

Therefore, any language-specific (as opposed to algorithmic) problem
with C code, no matter how severe, is technically merely a portability
issue. However, "non-portable" means different things to different
people, so it's better to be more specific, if you can be.
 
R

Richard Bos

arnuld said:
Just want to know, if someone uses void main() what word I should use (if
illegal is not the one).

"Stupid" would suffice, IMO. Outside of freestanding implementations,
there is no good reason to use void main().

Richard
 
W

WangoTango

"Stupid" would suffice, IMO. Outside of freestanding implementations,
there is no good reason to use void main().

Richard
OK, I'm confused.
First you say it is 'stupid' and then give an example of where it would
be applicable.
I have written many apps that NEVER return to the environment that
called them and, if they do, then any values that may be returned would
be meaningless.
Therefore, "void main(void)" is very applicable.

The main function takes to arguments and returns to meaningful data to
the calling environment, simple enough.
 
L

Lew Pitcher

OK, I'm confused.
First you say it is 'stupid' and then give an example of where it would
be applicable.
I have written many apps that NEVER return to the environment that
called them and, if they do, then any values that may be returned would
be meaningless.

So what? The C main() function is /defined by the language/ as returning an
integer value; the /language/ says nothing about whether or not the
execution environment (that which /calls main()/) will use or even
recognize the return value.

Therefore, "void main(void)" is very applicable.

No. I suggest that
int main(void)
{
/* do something */
return 0;
}
is applicable here.

OTOH, while
void main(void)
{
/* do something */
}
may work for you, it does so at risk of unexpected failure, /and/ violates
the definition (to the system) of the main() function.
The main function takes to arguments

sometimes. Sometimes not.
and returns to meaningful data to
the calling environment,

which void main(void) cannot do
simple enough.

You seem conflicted. You say that void main() is applicable, and then imply
that it is not.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
K

Keith Thompson

WangoTango said:
OK, I'm confused.
First you say it is 'stupid' and then give an example of where it would
be applicable.
I have written many apps that NEVER return to the environment that
called them and, if they do, then any values that may be returned would
be meaningless.
Therefore, "void main(void)" is very applicable.

The main function takes to arguments and returns to meaningful data to
the calling environment, simple enough.

You mean "no arguments" and "no meaningful data", right?

For a freestanding implementation, "void main(void)" may be valid, or
even necessary. A freestanding implementation isn't even required to
support the two forms of main() listed in the standard.

For a hosted implementation, using "void main(void)" is -- well, I
won't use the word "stupid", but it is foolish. If you don't need to
return a meaningful value to the environment, you can just return 0
(or, in C99, fall off the end of main() with the same result). If you
declare "void main(void)", the compiler is free to reject your
program, or to make it behave in some unpredictable manner. What
benefit makes that worthwhile?
 
W

WangoTango

So what? The C main() function is /defined by the language/ as returning an
integer value; the /language/ says nothing about whether or not the
execution environment (that which /calls main()/) will use or even
recognize the return value.



No. I suggest that
int main(void)
{
/* do something */
return 0;
}
is applicable here.

OTOH, while
void main(void)
{
/* do something */
}

more like
void main(void)
{
while(TRUE)
{

}
}
may work for you, it does so at risk of unexpected failure, /and/ violates
the definition (to the system) of the main() function.
Always works for me.
sometimes. Sometimes not.
We are not talking ALWAYS.
Depends on the environment.
Not every 'C' program runs on a PC.
which void main(void) cannot do EXACTLY


You seem conflicted. You say that void main() is applicable, and then imply
that it is not.

I am not conflicted in the least.
 
W

WangoTango

You mean "no arguments" and "no meaningful data", right?
Yep, don't know how the noggin pulled that one off.
Getting old is hell.
For a freestanding implementation, "void main(void)" may be valid, or
even necessary. A freestanding implementation isn't even required to
support the two forms of main() listed in the standard. Exactly.


For a hosted implementation, using "void main(void)" is -- well, I
won't use the word "stupid", but it is foolish. If you don't need to
return a meaningful value to the environment, you can just return 0
(or, in C99, fall off the end of main() with the same result). If you
declare "void main(void)", the compiler is free to reject your
program, or to make it behave in some unpredictable manner. What
benefit makes that worthwhile?
I agree.
I use the form "main(void)" with all of my current cross compilers, but
some of my older implementations insisted on "void main(void)" because
main never returns. Any attempt to fall off the bottom of the function
is just a jump to the beginning.
Anyway, I was just making the point that 'C' is run on a LOT of
platforms and not all of them return to any type of operating system.
 
K

Keith Thompson

WangoTango said:
On June 9, 2009 13:21, in comp.lang.c, WangoTango ([email protected])
wrote: [...]
Therefore, "void main(void)" is very applicable.

No. I suggest that
int main(void)
{
/* do something */
return 0;
}
is applicable here.

OTOH, while
void main(void)
{
/* do something */
}

more like
void main(void)
{
while(TRUE)
{

}
}
may work for you, it does so at risk of unexpected failure, /and/ violates
the definition (to the system) of the main() function.
Always works for me.

So far.

If you write:

void main(void) { while (1) { /* ... */ } }

then it's fairly likely to work on whatever system you're using, but
it might fail. Failure can take many forms, obvious or subtle. The
compiler can reject your program, your stack layouts can be messed up
because your code didn't allocate space for the return value, and so
forth. Failure can occur when you or somebody else ports the code to
a new system, to a new compiler on the same system, to a new version
of the same compiler, to the same compiler with different command-line
options, or just because it's Tuesday. If your program malfunctions
(and you happen to notice), and you complain to your compiler vendor,
the vendor can legimimately tell you that your code is invalid and
reject your bug report.

If you write:

int main(void) { while (1) { /* ... */ } }

then it *will* work on any conforming hosted implementation. None of
the above problems apply. Oh, and it's one less letter to type.

Can you explain again why void main is better?

None of this applies if you're using a freestanding implementation
that documents "void main(void)" as a valid entry point declaration.
If so, you could have saved us a lot of time by mentioning it.

[...]
I am not conflicted in the least.

You should be.
 
K

Keith Thompson

WangoTango said:
I agree.
I use the form "main(void)" with all of my current cross compilers, but
some of my older implementations insisted on "void main(void)" because
main never returns. Any attempt to fall off the bottom of the function
is just a jump to the beginning.
Anyway, I was just making the point that 'C' is run on a LOT of
platforms and not all of them return to any type of operating system.

"main(void)" is obsolete; C99 dropped the implicit int feature. For
compilers that support it, it's exactly equivalent to "int main(void)".

Falling off the bottom of the function should terminate the program.
Perhaps it's just being re-invoked?

In any case, since you're using cross compilers, you're probably using
freestanding implementations, in which case void main(void) is
perfectly valid if the implementation documents it, and this
conversation has been a waste of time that could have been avoided if
you'd just mentioned that fact earlier.
 
S

squeamz

On June 9, 2009 13:21, in comp.lang.c, WangoTango ([email protected])
wrote:
more like
void main(void)
{
  while(TRUE)
  {

  }

}

Always works for me.





We are not talking ALWAYS.
Depends on the environment.
Not every 'C' program runs on a PC.

You go, girl! This is SUCH a big issue with me. Some people just
don't get it; not everything is a nail, and sometimes "int main" is
just not the right tool for the job. This is an issue of colossal
importance, and we're damn lucky to have you here to fuel the debate.
My hat's off to you, sir!

Next up, gay marriage...
 
K

Keith Thompson

Jack Klein said:
Even more important, did you know that "...." is NOT LEGAL in English?

<OT>Not in this context, but it can be used as an ellipsis followed by
a period (or full stop if you prefer). But yes, some writers' mania
for inappropriate ellipses is both annoying and difficult to
explain.</OT>
 
W

WangoTango

WangoTango said:
"main(void)" is obsolete; C99 dropped the implicit int feature. For
compilers that support it, it's exactly equivalent to "int main(void)".

Falling off the bottom of the function should terminate the program.
Perhaps it's just being re-invoked?
Some do a jump back to the entry point of main one turns off the INTs
and executes a SLEEP instruction that halts execution until an interrupt
occurs, essentially halting execution until a reset.
In any case, since you're using cross compilers, you're probably using
freestanding implementations, in which case void main(void) is
perfectly valid if the implementation documents it, and this
conversation has been a waste of time that could have been avoided if
you'd just mentioned that fact earlier.

Sorry.....
 
G

Guest

just a general remark here. Why ask or discuss the question if you
aren't
interested in the answer? I'm not addressing the OP here.


nor does the C++ standard


"freestanding" has a special technical meaning as far as the C
standard
is concerned. "Hosted" implementations must provide the entire C
library
and are entered by a correctly defined main() function. "Freestanding"
implementations are only required to provide stdlib, stdef and float
(they can provide more if they want) and are entered "in an
implementation
defined manner (which could be main). clc usually discusses hosted
implementations unless stated otherwise.

"freestanding" are usually embedded systems. Though jokers
have said since windows programs are entered via WinMain() this
means windows is a freestandinging impl.

more like
void main(void)
{
  while(TRUE)
  {
Always works for me.
The main function takes [two?] arguments
sometimes. Sometimes not.
We are not talking ALWAYS.
Depends on the environment.
Not every 'C' program runs on a PC.

there are two definitions of main() that must be provided by the
standard
int main (void);
int main (int, char**); /* or equivalent */

an implementaion *may* provide others but if you use an alternative
your program becomes unportable. Why do this for such a little thing?

<snip>
 
R

Richard Bos

WangoTango said:
OK, I'm confused.
First you say it is 'stupid' and then give an example of where it would
be applicable.

Nope. For a hosted implementation, using void main() is stupid. For a
freestanding implementation, this entire discussion is of no import
whatsoever, since the article under discussion (5.1.2.2.1) only applies
to hosted implementations in the first place.
I have written many apps that NEVER return to the environment that
called them and, if they do, then any values that may be returned would
be meaningless.
Therefore, "void main(void)" is very applicable.

And what _does_ that return to the OS? Randomness, that's what. Very
nice in a makefile.

(Oh, you were talking about situations where you don't have an OS to
return to? Then you aren't in 5.1.2.2 at all, but in 5.1.2.1, so this
whole thread is not about you from the very first post.)

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top