473,657 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prototype of "main"

Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?
Thanks,
Neelesh

Nov 6 '05 #1
9 2181
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
Neelesh <ne***********@ gmail.com> wrote:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?


As I recall that particular case is implementation-defined. Check out
http://www.comeaucomputing.com/techtalk/#voidmain
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 6 '05 #2

Neelesh skrev:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?
For main only no return value corresponds to return 0. So this is a
very special situation. Your variety with five ints is non-conforming,
however.

/Peter

Thanks,
Neelesh


Nov 7 '05 #3
peter koch wrote:

Neelesh skrev:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?


For main only no return value corresponds to return 0. So this is a
very special situation. Your variety with five ints is non-conforming,
however.


It's not. An implmentation is required to accept a definition of main() with
either no parameters or the usual argc/argv ones, but it is free to accept
any other parameter list. Only the return type is always required to be
int.

Nov 7 '05 #4

Rolf Magnus skrev:
peter koch wrote:

Neelesh skrev:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?


For main only no return value corresponds to return 0. So this is a
very special situation. Your variety with five ints is non-conforming,
however.


It's not. An implmentation is required to accept a definition of main() with
either no parameters or the usual argc/argv ones, but it is free to accept
any other parameter list. Only the return type is always required to be
int.

Is that so? I am aware that there is the concept of a freestanding
implementation which has special liberties wrt the the parameters for
main. If that is what you mean, I believe that we agree and that my
wording was a bit to strong. If not, I am surprised if this weirdly
looking main is conforming.

/Peter

Nov 7 '05 #5
* peter koch -> Rolf Magnus:
I am aware that there is the concept of a freestanding
implementation which has special liberties wrt the the parameters for
main. If that is what you mean, I believe that we agree and that my
wording was a bit to strong. If not, I am surprised if this weirdly
looking main is conforming.


Freestanding implementation has nothing to do with it.

The standard requires int result type, and the standard requires support
for two specific signatures (unless its a freestanding implementation) .

The implementation is free to support additional signatures, if they all
have int result type.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 7 '05 #6

Alf P. Steinbach skrev:
* peter koch -> Rolf Magnus:
I am aware that there is the concept of a freestanding
implementation which has special liberties wrt the the parameters for
main. If that is what you mean, I believe that we agree and that my
wording was a bit to strong. If not, I am surprised if this weirdly
looking main is conforming.


Freestanding implementation has nothing to do with it.

The standard requires int result type, and the standard requires support
for two specific signatures (unless its a freestanding implementation) .

The implementation is free to support additional signatures, if they all
have int result type.


Hi Alf

Thanks - always nice to learn something new.

/Peter

Nov 7 '05 #7
peter koch wrote:

Alf P. Steinbach skrev:
The implementation is free to support additional signatures, if
they all have int result type.


Hi Alf

Thanks - always nice to learn something new.

Many for unix systems, it was (is) common to have main() declared as:

int main(int argc, char **argv, char **envp)
The third argument would give you the process's environment variable
list. This is so convenient that people often ask about it when their
implementation doesn't support it. See also:

http://www.eskimo.com/~scs/C-faq/q11.13.html


Brian
Nov 7 '05 #8

Greg Comeau wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
Neelesh <ne***********@ gmail.com> wrote:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?


As I recall that particular case is implementation-defined. Check out
http://www.comeaucomputing.com/techtalk/#voidmain


The program's behavior is undefined, because implementations are not
required to support that form of main.

A program that uses a feature for which the support is
implementation-defined in fact invokes undefined behavior.

I would say that the definition

int main(int a, int b, int c, int d, int e) { /* ... */ }

is /worse/ than one returning void! The reason is that no diagnostic is
required! A diagnostic is required if the return type is other than
int. (In that case, the C++ implementation can still translate and
execute the program anyway, but its behavior is undefined).

Anything other than int main() or int main(int, char **) runs by dumb
luck, or by means of being supported as an extension, which has nothing
to do with the standard other than being allowed with or without a
diagnostic, depending on whether the return type is int.

A long ago I wrote this:

http://users.footprints.net/~kaz/cppvoidmain.html

That document is based on the draft C++ standard, so it may be
inaccurate.

Nov 7 '05 #9
Kaz Kylheku wrote:
Greg Comeau wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
Neelesh <ne***********@ gmail.com> wrote:
Hi all,
it is strange that the following code compiles and runs (tested with
g++ 3.4.2)

#include <iostream>
using namespace std;

int main(int a, int b, int c, int d, int e)
{
cout << "hello " << endl;
}

Is C++ lenient about the prototype of main? Does it say anything if the
prototype is not one of those standard argc-argv-env kinds?
Is the program expected to run in such cases or this is simply by luck
that it runs properly?
As I recall that particular case is implementation-defined. Check out
http://www.comeaucomputing.com/techtalk/#voidmain


The program's behavior is undefined, because implementations are not
required to support that form of main.


Implementation-defined behavior is not undefined behavior. So without
knowing anything about the implementation, it is not possible to
conclude whether this program's behavior is undefined or not. It is
possible to determine whether the program's behavior is defined for a
given compiler, because all implementation-defined behavior must be
documented. So in this case, it is necessary only to consult the
"implementa tion-defined behavior" section of the compiler's
documentation to find out what it says about arguments to main.
A program that uses a feature for which the support is
implementation-defined in fact invokes undefined behavior.
No, it invokes the behavior that the implementation, and not the
Standard, has defined for it.
I would say that the definition

int main(int a, int b, int c, int d, int e) { /* ... */ }

is /worse/ than one returning void! The reason is that no diagnostic is
required! A diagnostic is required if the return type is other than
int. (In that case, the C++ implementation can still translate and
execute the program anyway, but its behavior is undefined).

Anything other than int main() or int main(int, char **) runs by dumb
luck, or by means of being supported as an extension, which has nothing
to do with the standard other than being allowed with or without a
diagnostic, depending on whether the return type is int.


The C++ standard specifically leaves some questions - such as the size
of a byte or additional declarations of main - up to each
implementation to decide on its own. It does so because it recognizes
that a "one-size-fits-all" approach is not always practicable for all
types of processors and OS'es. Instead it is better to have each
implementation be responsible for deciding these types of issues and
for documenting the decision. So any C++ program whose behavior is
defined by either the Standard or by the implementation is a valid and
perfectly acceptable C++ program.

Greg

Nov 8 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2993
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the package. Then I ran /usr/java2/bin/javac SelectSockets.java to make the SelectSockets.class file.
4
1945
by: Don | last post by:
Is there some way to redirect the "main" frame to another URL from within the "header" frame? Thanks, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
7
3985
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread. This would allow for other plugins and the main application to be free to do other work. I have written a little TestDriver for the plugin and am having some difficulty. If I don't use threads everything works just fine. If I do use threads, upon...
25
1903
by: mdh | last post by:
Hi all, Going quite methodically through K& R ( as some of you can attest to!), I have never seen a big diffference in declaring a function within "main" or "ahead" of it. Now, (p119, K&R II), the discussion states that "functions "whatever" " should be declared ahead of main. Is there a good reason for this? thanks.
12
7118
nomad
by: nomad | last post by:
Hi everyone; My Class has ended and I was not able to solve this problem in time, and I would still like to solve it. I got these error code. Exception in thread "main" java.lang.NullPointerException at ticketSales.TicketSales.makeEvent(TicketSales.java:185) at ticketSales.TicketInput.main(TicketInput.java:56) Anyway I have several Class This one is called TransAction
5
3855
by: Summercoolness | last post by:
if you are just a users among the 100s of users on a hosting machines, then if you do a include("/main.php"); it won't include the file but will say Failed opening required '/main.php' (include_path='.:/usr/local/nf/ lib/php') it seems ok to include("../main.php"); except the file will not work if moved from www.abc.com/movies to a new location of www.abc.com/movies/little-mermaid is there a better solution for
2
3950
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message "Main" Java.lang NullPointerException at Project1.sortingByZipCode<Project1.java:80> at Project1.main<Project1.java:31> Here is the Source Code
1
13718
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
3
7648
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34) at sortmergejoin.Main.main(Main.java:24) Java Result: 1"
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.