4 Replies - 6609 Views - Last Post: 28 January 2010 - 07:19 AM Rate Topic: -----

#1 p.sudhanshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 10

difference Between C' main() and C++ main()

Post icon  Posted 28 January 2010 - 06:56 AM

what is the difference between main function in C and C++?
why we use void in C++ but not in C
Is This A Good Question/Topic? 0
  • +

Replies To: difference Between C' main() and C++ main()

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: difference Between C' main() and C++ main()

Posted 28 January 2010 - 07:06 AM

C is a procedure oriented language.
C++ is object-oriented language

'main()' is C returns void
in C++, 'main()' returns an integer

And if you use 'void main()', you use it in C (in the antique Turbo C), not in C++. In C++ you should always use 'int main()', though using 'void main()' won't result in an error, but it's not standard. Why should you use 'void main()' in C++: http://www.gidnetwork.com/b-66.html

This post has been edited by sarmanu: 28 January 2010 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: difference Between C' main() and C++ main()

Posted 28 January 2010 - 07:09 AM

you DON'T!

Both C and C++ the syntax for main() should be:

int main()

or

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

It is lazy programmers who use void main() which is NOT part of the standard and is just "allowed" by compilers... mostly because it was common in C and they don't want to break older programs.

You should ALWAYS use one of the two syntax examples given above. Your compile may let you get away with other signatures:

main() { }

void main() { }

void main(int argc, char *args[]) { }

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

int main(int argc, char **argv, char **envp, char **apple)

All of these are non-standard and specific to your platform & compiler.



Quote

'main()' is C returns void
in C++, 'main()' returns an integer

Wrong... on most platforms (DOS, Unix, linux, MacOS, VMS, etc etc.) ALL programs have an exit code -- so main ALWAYS returns a value. Even if you don't tell it to. (when you use void main() the compiler just puts in a return 0 for you -- i.e. its only for lazy programmers who can't be bothered to type out "return 0;")
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: difference Between C' main() and C++ main()

Posted 28 January 2010 - 07:12 AM

I have learned that 'void main()' does not return anything. And how can a 'void' return something? Maybe main() is a special case? Anyways, you should not use 'void main()' at all. Period.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: difference Between C' main() and C++ main()

Posted 28 January 2010 - 07:19 AM

Quote

I have learned that 'void main()' does not return anything. And how can a 'void' return something?

See you have learned a lie. the function main() ALWAYS returns a value because the OS requires it... So, void main() is a lie, because it WILL return a value (because the OS says so) even though you told it NOT to.

Now do you see why it should be avoided? Its broken. Its a lie. Thus it did not make it into the standard (actually I have no idea why it did not make it into the standard... but this is my guess).

normally though you are right, a void function should not return a value, but main() is REQUIRED to... so, it does. The only reason you are allowed to use void main() at all, is because compile manufacturers didn't want to break older programs (back from the days when C was used to WRITE operating systems! rather than take orders from them). Unfortunately the idiom (and it is an idiom) has persisted though the ages. Many compiler manuals use void main() in their examples!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1