The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-21-2007, 11:01 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Why int main() ???

Code:
int main()
{
   cout << "Hello! The program starts in main()."
        << endl;
   line();
   message();
   line();
   cout << "At the end of main()." << endl;
   
   return 0;
}
anybody know what's int means on the main method? Does that represent anything? Why is it int? I'm pretty sure that's not return value right??? aish...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 06-22-2007, 04:33 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Int means integer, main(), is returning an Integer, hence the int. When It says return, that means that when you say "return 0" in your code. So basically, until it gets to the return statement, it knows that it's job isnt done yet, when it sees that, the "return 0" it knows it has finished.

hope that helps

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 06-22-2007, 08:07 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
oh yeahhh!!! I just saw that there's a return 0 there sigh.. let me try with void then thanx a lot Lee
I'm now playing around with C++

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 06-22-2007, 08:43 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
your welcome, im a fellow noob at C++, learning as i go i suppose, glad i can offer help and im not totally useless at this area XD

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 06-26-2007, 09:44 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I think void main() is out of date. Much of programmers suggest that using
int main() instead of void main(). Honestly, I don't know why too. Anyway,
you don't need to return any value in main() function. It is exceptional.
However, it is recommented to return value.

From siLenTz
Just Sharing my Idea...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-07-2007, 01:21 PM
Konr Konr is offline
Novice
Join Date: Jul 2007
Posts: 10
iTrader: (0)
Konr is on a distinguished road
The C++ language definition requires that main return an int. To do otherwise is incorrect C++, but many compilers allow it for various reasons, such as tradition, backwards-compatibility and because allowing it extends the language in a way that doesn't affect any other part of it.

It is customary for the return value to indicate success, usually by returning zero, or to give an error code that can be used to debug.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to Konr For This Useful Post:
HelloWorld (07-07-2007), TeraTask (07-07-2007)
  #7 (permalink)  
Old 07-07-2007, 04:48 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Would the compiler return some kind of an exception if we don't put int return value in the main method? Well, I personally interested in C# more than C++ though for ASP.NET, but I'm just wondering at least to add up for my knowledge

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-07-2007, 05:09 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Would the compiler return some kind of an exception if we don't put int return value in the main method? Well, I personally interested in C# more than C++ though for ASP.NET, but I'm just wondering at least to add up for my knowledge
A compliant compiler will either display an error and refuse to compile the program or compile the program after issuing a warning about the practice.

As somebody already mentioned, returning an integer rather than nothing at all is better because then you can determine whether the program exited due to errors. In fact, on many Linux machines, you can print out a special variable to determine the exit success:
g++ -std=c++98 -o myprog myprog.cpp && myprog
# the program output is here
echo $?
# if $? shows 0, then myprog exited fine (it exited with 0 errors)

As a side note, this is contrary to many other programming implementations, such as the Windows API, where if you return 0 from a function, that means that the function did not exit successfully (did you exit successfully? 0/FALSE).

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-08-2007)
  #9 (permalink)  
Old 07-08-2007, 09:18 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
In fact, on many Linux machines, you can print out a special variable to determine the exit success:
g++ -std=c++98 -o myprog myprog.cpp && myprog
# the program output is here
echo $?
# if $? shows 0, then myprog exited fine (it exited with 0 errors)
do you also create linux application with C++
Cuz I've also been wondering with this a while back ago to what language do you use to create a linux application..? Because as you see, windows is really popular because many developers are program their application using C++ or whatever else, while I don't see any specific language that's dedicated to create a linux application. That's one part that my friend and me researching about for his graduate studies

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-08-2007, 09:23 AM
Konr Konr is offline
Novice
Join Date: Jul 2007
Posts: 10
iTrader: (0)
Konr is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Would the compiler return some kind of an exception if we don't put int return value in the main method?
Well, it depends on which compiler you are using. If you want to know for sure for any given compiler, do it and see what happens. Some will refuse to compile, some will give you a warning but compile anyway, some will compile without mentioning that you've coded against the standard. Needless to say, a compiler that doesn't tell you when you're making mistakes and compiles around it is not something to wish for.

An 'exception' occurs during run-time. Your compiler will give you errors or warnings.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Konr For This Useful Post:
HelloWorld (07-08-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 10:49 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50