Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 1st, 2003, 02:09 PM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning C by Ivor Horton

I purchased this book a while ago and after coding for a while I realized that the main function "void main()" is looked down upon by many coders. This book teaches based on that though. Why didn't you teach
Code:
int main()
{
   return 0;
}
Isn't that the correct way to actually do that? Just wondering
Reply With Quote
  #2 (permalink)  
Old July 17th, 2003, 01:41 PM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not an expert, so my answer might be wrong.

There is NO standard (set) prototype for main() function. When your exe runs, it will start with function with name main, doesn't matter the return type or arguments. If you have more than 1 main(), your compiler might warn/yell at you.

The most complete one is:
int main(int argc, char* argv[], char *envp[]);
   where: argc is how many arguments pass to exe (including the exe filename)
          argv is each arguments pass to exe
          envp is pointer to array of env string (specific to Windows)

NOTE: main starts and ends the program. You can use the return value to check the end status of your program. Remember that you can start another exe from another exe, so you can check how the called exe ends (eg returnVal<0 means fatal error, returnVal=0 means end successfully, returnVal=1 means end with minor error). Don't ask me the coding, I don't know, never use it before.


Since I was first trained on UNIX, I use:
int main(int argc, char* argv[]);
eg. C:> mytestProg.exe foo bar
   argc will be 3
   argv[0] will be "mytestProg.exe"
   argv[1] will be "foo"
   argv[2] will be "bar"

cheers,
&nbsp;&nbsp;Johan
Reply With Quote
  #3 (permalink)  
Old July 24th, 2003, 10:05 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, from what I know, main() function should return an int value as defined by ISO standart. And indeed if you try to just type in main() or void main() in GCC, it will give you some warning about ISO reference thingies. Since int main() function should return an inretger value, return 0 is used.

Reply With Quote
  #4 (permalink)  
Old August 1st, 2003, 05:44 PM
Authorized User
 
Join Date: Jul 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default


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

  Are ANSI / ISO C. They are equivlant expressions. Kerrigan reomments the later in his book ... I use the former believing it to more clearly denote the data type as a pointer to a pointer.

void main(void)
 ||
void main()

  Are premissable C++ entry points. That is why say, gcc may give you an error while Visual Studio will not. g++, or gcc with appropriate command line arguements will compile the C++ constructs.

 It is important to note that while

  void main()
    &&
  void main(void)

  are equivlant constructs in C++ they are not so in C. This is because C++ has stronger type checking and interperts the empty parenthasis as meaning (void) while C interperts them as meaning unspecified argument list.

 In C++ the argument does matter ... void main(char ch) would not be linked as the entry point by default. The C++ compiler would mangle the name into something else...

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

  are valid C++ constructs. Some operating systems specify more -- UNIX for example also has a main that takes a pointer to a list of environment variables after **argv, for example.

Regards,
Meredith Shaebanyan



Reply With Quote
  #5 (permalink)  
Old August 2nd, 2003, 08:36 PM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
void main(void)
||
void main()

Are premissable C++ entry points.
Apparently many people do not agree with you:

What is correct: "void main()" or "int main()"?

According to the ANSI/ISO standard, the correct forms are two:

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

Even though "void main()" will probably compile (depends on the compiler), it is not correct!

http://www.cpp-home.com/
--------------

void main(void) - the Wrong Thing

http://users.aber.ac.uk/auj/voidmain.shtml

-----------------

void main() is not legal in C++ but is legal in C.
You've come to this page because you've said something similar to
void main() is not legal in the C language. main() is required to return int.
This is the Frequently Given Answer to that false assertion.

The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. But the ISO C Standard (ISO/IEC 9899:1999) actually does not. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.

In my opinion, this is a defect in the C Standard that needs fixing with a corrigendum.

http://homepages.tesco.net/~J.deBoyn...void-main.html
----------------

http://www.delorie.com/djgpp/v2faq/faq22_25.html

-----

http://faq.cprogramming.com/cgi-bin/...&id=1043284376

Reply With Quote
  #6 (permalink)  
Old August 5th, 2003, 05:08 PM
Authorized User
 
Join Date: Jul 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default

7stud

 Fascinating reading indeed though the second link only seems to cover void as a return type. I confess, I didn't drill down very far into the first link, but I was under the impression (and I haven't checked this in quite some time) that according to the [C++] standard

int func();

  is equivlant to

int func(void);

  due to C++'s stronger type-checking (which is one of those little compatibilty breakers with standard C ) and that given that

int main(void)

  *is* legal syntax and

int main()

   is equivlant, would it not too be legal? hrm....this is why we have linker flags to sepcify entry points

Reply With Quote
  #7 (permalink)  
Old August 6th, 2003, 03:51 PM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:void main(void)
||
void main()

Are premissable C++ entry points.
Quote:
quote:void main(int argc, char **argv)
...
void main(void)
...
are valid C++ constructs.
Your claim that using void as the return type is allowed under the C++ standard is clearly erroneous.

Quote:
quote:
int main()
{
   return 0;
}

Isn't that the correct way to actually do that? Just wondering
Yes.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Code - Wrox - Beginning JAVA - Ivor Horton ponguru Java Databases 3 May 18th, 2006 12:30 PM
Java 2 Beginning / Ivor Horton JESwan BOOK: Beginning Java 2 3 May 6th, 2005 07:25 AM
Ivor Horton : Beginning Java 2 JDK 1.4 lingprash BOOK: Beginning Java 2 0 August 6th, 2004 01:56 AM
Beginning Java 2 SDK by Ivor Horton problems deviad Java GUI 1 November 27th, 2003 07:38 AM
Beginning C++ Ivor Horton Okami All Other Wrox Books 2 October 30th, 2003 05:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.