question on main( )

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Suppose the return type of main is void.

void main(void)
{
...

return;
}

Is the compiler supposed to generate warning or error according to
Standard C.

I use return type as int only. But for knowledge sake I am asking this
question.
 
S

santosh

Suppose the return type of main is void.

void main(void)
{
...

return;
}

Is the compiler supposed to generate warning or error according to
Standard C.

Yes. A diagnostic is required.
 
F

Flash Gordon

santosh wrote, On 07/03/07 03:49:
Yes. A diagnostic is required.

C&V? I know the standard specifies exactly two variants for main (and
allows the implementation to define more) but I don't believe that it is
in a constraint, so I think it is undefined behaviour rather than
requiring a diagnostic.
 
K

Keith Thompson

santosh said:
Yes. A diagnostic is required.

I don't believe so. C99 allows the main function to be defined in one
of the two normal ways "or in some other implementation-defined
manner". Even if the implementation doesn't define another manner for
declaring main, the requirement is not a constraint or a syntax rule,
so no diagnostic is required; it invokes undefined behavior.

The rules are similar for C90, except that the older standard doesn't
explicitly allow implementation-defined forms.
 
S

santosh

Flash said:
santosh wrote, On 07/03/07 03:49:

C&V? I know the standard specifies exactly two variants for main (and
allows the implementation to define more) but I don't believe that it is
in a constraint, so I think it is undefined behaviour rather than
requiring a diagnostic.

Flash, (and Keith), thanks for the correction. That's what happens
when you work too much with a single compiler, (gcc), and not read the
Standard carefully.
 
R

Richard Heathfield

(e-mail address removed), India said:
Suppose the return type of main is void.

The return type of main is int. Supposing it is void is like supposing
the earth is a cube. It simply ain't so.
void main(void)
{
...

return;
}

Is the compiler supposed to generate warning or error according to
Standard C.

No, the compiler is under no obligation to diagnose an incorrect return
type, any more than it is obliged to diagnose i=i++ or fflush(stdin),
but void main is just as wrong as either of these.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top