Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Legality of void main in C

Reply
Thread Tools

Legality of void main in C

 
 
prasoonthegreat@gmail.com
Guest
Posts: n/a
 
      06-09-2009
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-09-2009
writes:
> 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).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      06-09-2009
> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

>> writes:
>> 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.



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



--
www.lispmachine.wordpress.com
my email is @ the above blog.


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-09-2009
arnuld wrote:
>> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

>
>>> writes:
>>> 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.

>
>
> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-09-2009
arnuld <> writes:
>> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

>
>>> writes:
>>> 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.

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


Schildt.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
prasoonthegreat@gmail.com
Guest
Posts: n/a
 
      06-09-2009
On Jun 9, 9:06*pm, Keith Thompson <ks...@mib.org> wrote:
> arnuld <sunr...@invalid.address> writes:
> >> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:

>
> >>> prasoonthegr...@gmail.com writes:
> >>> 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.

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

>
> Schildt.
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"


Thanks!!!!
 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      06-09-2009
On Jun 9, 12:07*pm, prasoonthegr...@gmail.com wrote:
> On Jun 9, 9:06*pm, Keith Thompson <ks...@mib.org> wrote:
> > arnuld <sunr...@invalid.address> writes:
> > > Just want to know, if someone uses void main() what word I should use (if
> > > illegal is not the one).

>
> > Schildt.

>
> Thanks!!!!


That was a joke.

REH
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-09-2009
arnuld <> wrote:

> On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
> > writes:


> >> 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.

>
> 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
 
Reply With Quote
 
WangoTango
Guest
Posts: n/a
 
      06-09-2009
In article <>, says...
> arnuld <> wrote:
>
> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
> > > writes:

>
> > >> 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.

> >
> > 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
>

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.


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      06-09-2009
On June 9, 2009 13:21, in comp.lang.c, WangoTango ()
wrote:

> In article <>, says...
>> arnuld <> wrote:
>>
>> > On Mon, 08 Jun 2009 23:44:30 -0700, Keith Thompson wrote:
>> > > writes:

>>
>> > >> 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.
>> >
>> > 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
>>

> 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. ------


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Legality of void main in C prasoonthegreat@gmail.com C Programming 2 06-09-2009 11:56 PM
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57