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
 
N

Nathan Stoddard

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

You've pretty much answered your own question:
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.

Other forms of main are implementation-defined. Most compilers support
main with a return type of void, but they will probably give you a
warning if you use it.
 
N

Nate Eldredge

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

It depends on what you mean by "legal". Implementations are allowed to
accept it, but not required to. If you include it in your code, it will
work on those machines that accept it, and fail in some unknown manner
on those that do not. Most people would say it is not "legal" in
general to use `void main' in code, because it isn't guaranteed to work
on all conforming implementations.
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 .....

Right, some implementations can't deal with it. Therefore, if you want
your code to be portable to those implementations, you should not use it.
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top