Topic: main() function declaration


Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/17
Raw View
Ron Natalie wrote:
>
> David Satz wrote:
> >
> > I was once taught (by a Microsoft staff instructor back in the days
> > when they taught their own C/C++ language courses) that if code
> > compiles, it is correct--that the compiler is the final arbiter of the
> > language.  But it should be quite clear that this doctrine is false.
>
> The fact that Microsoft has that attitude is indicative of the
> defect in their product design policy.

Just because one instructor made that statement at one time does
not mean it was in any way official, or even the opinion of anyone
else at Microsoft.

I'm no fan of Microsoft, but generalizing from one datum
isn't fair, or even helpful.

OTOH, Bill Gates has been quoted as saying in at least one interview
that MS products don't have bugs; they have features, and get
improvements from release to release. I guess you have to consider
that an official attitude.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jim Cobban <thesnaguy@hotmail.com>
Date: 1999/11/18
Raw View
This is a multi-part message in MIME format.
--------------3F33FFA301380ADCC11E199B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Ron Natalie wrote:

> However the compiler is free to support any other signatures as long
> as the return type is int.  For example, many UNIX compilers support
>         int main(int argc, char* argv[], char** env)
>
> >
> > I am at present using a MSVC compiler and my compiler does not flag any
> > error even when i use the following for the declarations of main
>
> MSVC doesn't even pretend to comply with the standard.
> >
> > 1. int main(int,char*,char*,char*)
>
> This is OK (though probably ought to give at least a warning because
> I doubt the compiler does anything sane here.  It most likely
> reinterprets the char*[] arg as char* and puts garbage in the
> subseuquent arguments.

According to the standard the compiler can accept other "implementation
defined" signatures.  That does not say that the compiler should accept any
old garbage that the programmer supplies, only that it can accept other
specific signatures that have a meaning to that compiler implementation.  When
it does accept another signature it should document exactly what that
signature means.

>
>
> NOTE THAT JUST BECAUSE A COMPILER DOESN'T ISSUE AN ERROR, DOESN'T
> MEAN IT'S GOING TO WORK.

However I feel you would agree that for a compiler to accept a line of code
KNOWING that the result will not work, is a design flaw even if the letter of
the standard does not specifically demand a diagnostic.
--
Jim Cobban   jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438


--------------3F33FFA301380ADCC11E199B
Content-Type: text/x-vcard; charset=us-ascii;
 name="thesnaguy.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Jim Cobban
Content-Disposition: attachment;
 filename="thesnaguy.vcf"

begin:vcard
n:Cobban;James
tel;fax:+1-613-592-9438
tel;home:+1-613-592-9438
x-mozilla-html:FALSE
url:http://www.magma.ca/~jcobban
version:2.1
email;internet:thesnaguy@hotmail.com
title:Consultant
adr;quoted-printable:;;34 Palomino Dr.=0D=0A;Kanata;ON;K2M 1M1;Canada
fn:Jim Cobban
end:vcard

--------------3F33FFA301380ADCC11E199B--



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: eds <edsantos@cxp.com>
Date: 1999/11/15
Raw View
    argv and argc are there all the time. The compiler will supply a hidden one
if they are not declared. By the other side, main() is able to accept any number
of arguments, so it can accept char* or int with no complaint. Some compilers
also accept main() without the returning type to make it easy on non-ANSI C
code.

"Swaminathan, Srikanth (CTS)" wrote:

> Hi All,
>
> Excuse me if this question is not exactly relevant to this group. Since C is
> a subset of C++, I am posting this here. This question is relevant to c++
> also.
>
> I would like to know if there is any published declaration for the entry
> point function main I have heard of it having the following form
> int main(int argc, char* argv[]);
>
> I am at present using a MSVC compiler and my compiler does not flag any
> error even when i use the following for the declarations of main
>
> 1. int main(int,char*,char*,char*)
>
> 2. float main(int, char*, char*,int);
>
> How is my compiler able to accept both these???
>
> Thanx in advance
>
> Srikanth


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jack Klein <jackklein@att.net>
Date: 1999/11/16
Raw View
On 13 Nov 99 21:39:13 GMT, "Swaminathan, Srikanth (CTS)"
<SSrikan@CHN.CTS-CORP.COM> wrote in comp.std.c++:

> Hi All,
>
> Excuse me if this question is not exactly relevant to this group. Since C is
> a subset of C++, I am posting this here. This question is relevant to c++
> also.
>
> I would like to know if there is any published declaration for the entry
> point function main I have heard of it having the following form
> int main(int argc, char* argv[]);
>
> I am at present using a MSVC compiler and my compiler does not flag any
> error even when i use the following for the declarations of main
>
> 1. int main(int,char*,char*,char*)
>
> 2. float main(int, char*, char*,int);
>
> How is my compiler able to accept both these???
>
> Thanx in advance
>
> Srikanth

Both C and C++ are governed by ANSI/ISO standards, not by Microsoft or
any other particular compiler vendor.  Both languages require that
main() be defined with a return type of int for hosted environments.
Both language require that all conforming implementations for hosted
environments to accept the following two forms:

int main(void)
int main(int argc, char *argv[])

Both languages also allow the arguments to main() to be
implementation-defined, to allow for the common extension:

int main(int argc, char *argv[], char *envp[])

and other such.  The definition above was commonly used on UNIX,
MS-DOS, Windows, and probably some other systems, although it is
deprecates.

The Microsoft compiler is particularly sloppy about main, and show
"void main()" in almost all of their help files.  Most other compilers
will issue an error or warning if they see an incorrect definition of
main() and they are set to their standard conforming mode.

When set to strictly conforming mode, gcc issues a warning and
Borland's compilers issue an error and refuses to compile them.

Jack Klein
--
Home: http://jackklein.home.att.net
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/16
Raw View
"Swaminathan, Srikanth (CTS)" wrote:
>
> Hi All,
>
> Excuse me if this question is not exactly relevant to this group. Since C is
> a subset of C++, I am posting this here. This question is relevant to c++

C is not exactly a subset of C++. In this case, C accepts one form of
main() that is illegal in C++.

> also.
>
> I would like to know if there is any published declaration for the entry
> point function main I have heard of it having the following form
> int main(int argc, char* argv[]);
>
> I am at present using a MSVC compiler and my compiler does not flag any
> error even when i use the following for the declarations of main
>
> 1. int main(int,char*,char*,char*)
>
> 2. float main(int, char*, char*,int);

In C you can use:

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

in both languages you can use the equivalent definition:

 int main() { /* ... */ }

or alternatively,

 int main(int argc, char* argv[]) { /* ... */ }

However, implementations are allowed to accept additional forms, so long
as all accepted forms have a return type of 'int'. You're second example
breaks that requirement.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: John_Maddock@compuserve.com (John Maddock)
Date: 1999/11/16
Raw View
>I would like to know if there is any published declaration for the entry
>point function main I have heard of it having the following form
>int main(int argc, char* argv[]);

There are only two legal forms:

int main() {....}

and

int main(int argc, char* argv[]) {....}

anything else is illegal (or at least undefined) according to the
standard.

>
>I am at present using a MSVC compiler and my compiler does not flag any
>error even when i use the following for the declarations of main
>
>1. int main(int,char*,char*,char*)
>
>2. float main(int, char*, char*,int);
>
>How is my compiler able to accept both these???

Either complain or find yourself a better compiler vendor...


John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1999/11/16
Raw View
"Swaminathan, Srikanth (CTS)" wrote:

> I would like to know if there is any published declaration for the entry
> point function main I have heard of it having the following form
> int main(int argc, char* argv[]);

The compiler must support at least the above and
int main();

However the compiler is free to support any other signatures as long
as the return type is int.  For example, many UNIX compilers support
 int main(int argc, char* argv[], char** env)

>
> I am at present using a MSVC compiler and my compiler does not flag any
> error even when i use the following for the declarations of main

MSVC doesn't even pretend to comply with the standard.
>
> 1. int main(int,char*,char*,char*)

This is OK (though probably ought to give at least a warning because
I doubt the compiler does anything sane here.  It most likely
reinterprets the char*[] arg as char* and puts garbage in the
subseuquent arguments.

NOTE THAT JUST BECAUSE A COMPILER DOESN'T ISSUE AN ERROR, DOESN'T
MEAN IT'S GOING TO WORK.  There's a whole slew of things that the
compiler isn't even required to generate an error for (even when
it's not going to work right).

>
> 2. float main(int, char*, char*,int);

A diagnostic should be issued for the above.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "David Satz" <DSatz@msn.com>
Date: 1999/11/16
Raw View
Swaminathan, Srikanth (CTS) <SSrikan@CHN.CTS-CORP.COM> wrote in message
news:697D1663ABD6D1119D880060B0B54563018264CD@ctsinwcbsxua...

> I am at present using a MSVC compiler and my compiler does not flag any
> error even when i use the following for the declarations of main

 ... [some gnarly examples] ...

> How is my compiler able to accept both these???


By default, the MSVC compiler supports "Microsoft language
extensions."  In that mode, the compiler does not enforce strict
compliance of a program with the language standard.

I was once taught (by a Microsoft staff instructor back in the days
when they taught their own C/C++ language courses) that if code
compiles, it is correct--that the compiler is the final arbiter of the
language.  But it should be quite clear that this doctrine is false.

(By the way, it may perhaps have been more appropriate to post
your question on comp.lang.c++.moderated, though it certainly
does bear on standardization.)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: 1999/11/16
Raw View
Swaminathan, Srikanth (CTS) wrote...
> I would like to know if there is any published declaration for the entry
> point function main I have heard of it having the following form
> int main(int argc, char* argv[]);

Yes; in Standard C++ (ISO/IEC 14882), on page , main is defined as:

    int main();

or

    int main(int argc, char * argv[]);

The return type must always be int, but the arguments can be implementation
defined, so long as the two definitions above are legal. So...

> 1. int main(int,char*,char*,char*)

Is a legal extension to C++ (and C)

> 2. float main(int, char*, char*,int);

Is not legal Standard C++, because it has a return type of float.

> How is my compiler able to accept both these???

Microsoft pays little attention to Standard C++; in fact, they have actively
used the excuse of "backward compataibility" to defend their lack of a
Standard C++ implementation. In general, Visual C++ 6.0 SP3 is about 85% of
Standard C++. At this time, however, *no* C++ compiler implements the full
Standard.

--
  *     Scott Robert Ladd
  *     Coyote Gulch Productions - http://www.coyotegulch.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1999/11/16
Raw View


David Satz wrote:
>
> By default, the MSVC compiler supports "Microsoft language
> extensions."  In that mode, the compiler does not enforce strict
> compliance of a program with the language standard.
>
It doesn't even enforce it when you use the /Za flag to disable
the extensions.  All that does is disable the extensions, but it
doesn't do anything to fix the fact that the compiler doesn't
purport to comply with the standard.

> I was once taught (by a Microsoft staff instructor back in the days
> when they taught their own C/C++ language courses) that if code
> compiles, it is correct--that the compiler is the final arbiter of the
> language.  But it should be quite clear that this doctrine is false.

The fact that Microsoft has that attitude is indicative of the
defect in their product design policy.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/16
Raw View
eds wrote:
>
>     argv and argc are there all the time. The compiler will supply a hidden one
> if they are not declared. By the other side, main() is able to accept any number
> of arguments, so it can accept char* or int with no complaint. Some compilers
> also accept main() without the returning type to make it easy on non-ANSI C
> code.
>

What you say might be true for some particular implementation, but
it does not correspond to any provisions of the C++ standard.
Portable code cannot rely on such features.

The standard says that
- main must have a return type of int
- the implementation must allow the following two definitions of main:
 int main() { ... }
 int main(int argc, char* argv[]) { ... } // or equivalent

An implementation is allowed but not required to accept other
definitions of main.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1999/11/16
Raw View


John Maddock wrote:
>
>
> anything else is illegal (or at least undefined) according to the
> standard.
>
Sorry, that statement is incorrect.  The two you quote are the
only "guaranteed" forms.  The compiler is free to support (and
as I pointed out most UNIX compilers do) other signatures.

Other forms (as long as they return int) are allowed to be
defined by the implementation.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/16
Raw View
John Maddock wrote:
>
> >I would like to know if there is any published declaration for the entry
> >point function main I have heard of it having the following form
> >int main(int argc, char* argv[]);
>
> There are only two legal forms:
>
> int main() {....}
>
> and
>
> int main(int argc, char* argv[]) {....}
>
> anything else is illegal (or at least undefined) according to the
> standard.

No - the standard specifically allows other implementation-defined
forms, as long as the return type is 'int'.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "maxim" <maxk@NOSPAMtouro.edu>
Date: 1999/11/17
Raw View
VC also accepts void main();
I do not think this is standard-comliant behavoir.
max.

Swaminathan, Srikanth (CTS) wrote in message
<697D1663ABD6D1119D880060B0B54563018264CD@ctsinwcbsxua>...
>Hi All,
>
>Excuse me if this question is not exactly relevant to this group. Since C
is
>a subset of C++, I am posting this here. This question is relevant to c++
>also.
>
>I would like to know if there is any published declaration for the entry
>point function main I have heard of it having the following form
>int main(int argc, char* argv[]);
>
>I am at present using a MSVC compiler and my compiler does not flag any
>error even when i use the following for the declarations of main
>
>1. int main(int,char*,char*,char*)
>
>2. float main(int, char*, char*,int);
>
>How is my compiler able to accept both these???
>
>Thanx in advance
>
>Srikanth
>---
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Swaminathan, Srikanth (CTS)" <SSrikan@CHN.CTS-CORP.COM>
Date: 1999/11/13
Raw View
Hi All,

Excuse me if this question is not exactly relevant to this group. Since C is
a subset of C++, I am posting this here. This question is relevant to c++
also.

I would like to know if there is any published declaration for the entry
point function main I have heard of it having the following form
int main(int argc, char* argv[]);

I am at present using a MSVC compiler and my compiler does not flag any
error even when i use the following for the declarations of main

1. int main(int,char*,char*,char*)

2. float main(int, char*, char*,int);

How is my compiler able to accept both these???

Thanx in advance

Srikanth
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: brahms@mindspring.com (Stan Brown)
Date: 1999/11/15
Raw View
[This followup was also e-mailed to the cited author for speed -- please
follow up in the newsgroup.]

SSrikan@CHN.CTS-CORP.COM (Swaminathan, Srikanth (CTS)) wrote in
comp.std.c++:
>I am at present using a MSVC compiler and my compiler does not flag any
>error even when i use the following for the declarations of main
>
>1. int main(int,char*,char*,char*)
>
>2. float main(int, char*, char*,int);
>
>How is my compiler able to accept both these???

It's buggy. Or your compiler maker doesn't care about conforming to the
standard. Or both.

MSVC also accepts "void main()". In addition, it also fails to supply a
"return 0;", as required by the standard, when you correctly declare
main.

reference in the C++ standard: [basic.start.main] 3.6.1 Main function:
 It shall have a return type of type int, but otherwise
 its type is implementation   defined. All implementations
 shall allow both of the following definitions of main:
  int main() { /* ... */ }
 and
  int main(int argc, char* argv[]) { /* ... */ }
and skipping to paragraph 5 of the same section:
 If control reaches the end of main without encountering a
 return statement, the effect is that of executing
  return 0;

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                http://www.mindspring.com/~brahms/
C++ FAQ Lite: http://www.cerfnet.com/~mpcline/c++-faq-lite/
the C++ standard: http://webstore.ansi.org/ansidocstore/default.asp
more FAQs: http://www.mindspring.com/~brahms/faqget.htm


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]