Jump to content
  • Advertisement
Sign in to follow this  
bluefox25

Question about the main function

This topic is 4146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Which version is better? My textbook writes the main function as:
#include <iostream>
using namespace std;//Same ? applies to namespace
int main(){
....
return 0;
}
I have seen other books and tutorials use:
void main(some arguments){}

Just curious which is best to use? I have gotten use to the first method, will this pose a problem as I go on to Data Structures and the rest?

Share this post


Link to post
Share on other sites
Advertisement
If you don't care about the command line, use

int main();

If you want to have parameters passed to your program, use

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

In most cases the first version works just fine.

Share this post


Link to post
Share on other sites
Basically, it comes down to whether or not you want your program to accept "command line arguments". If you declare...

int main() { ... }

You're indicating that you have no use for arguments coming into the app. If you do want to be able to use arguments, then indicate those in the function header.

An example might be something like running "unzip.exe" from the command line. Not useful unless you can also indicate what you want unzipped, such as "unzip.exe myfile.zip". That's where those passed in arguments would be used.

{Edit}I was too slow... lol what nmi said is perfect :) {/Edit}
Hope this helps,
Scorp

Share this post


Link to post
Share on other sites
Yes it did thanx. How do you delete this thread being that my ? has been answered!!
Oh wait
What about the namespace thing? Declaring it above the main or or using the std:: ...

Isn't it easier to just declare outside the main so that all functions have access to it?

Share this post


Link to post
Share on other sites
After a program is executed and finishes it will return to the OS a return code. This is defined by what main(...) returns. So if main were to return an int, it could exit with return code -1, 0, n. If main were to return void, then I believe the program just returns 0.

Return codes were a useful way of debugging and checking the execution of a command line program under a unix environment, but given todays applications this is more of a history lesson than something practical. I know you can check for them under windows, but I just don't know how.

Hope I didn't make this too complicated but in C++ you can build frameworks to be encapsulated into a namespace. This is useful for identifying what it is you really want to work with. For example, in the package Gdk we have two classes that are in different sub-packages but share the same name. To specify which DateTime we want to use, we have to use the 'using class' deceleration.

using namespace Gdk;
using namespace Gdk::Sql;

// Uncomment one to specify which DateTime we want to use by default.
// using Gdk::DateTime;
// using Gdk::Sql::DateTime;

DateTime myTime; // C++ has no idea which DateTime to use.

// or

Gdk::DateTime myDateTime;

Share this post


Link to post
Share on other sites
Don't delete the thread, people in the future may have this exact question, and may go to do a search for it - if its around then they can answer their own questions.

Share this post


Link to post
Share on other sites
Yep, what MrRage said.

If it helps to have a direct example from what MrRage posted, consider this. Let's say for some reason I don't like the provided library of functions that have been placed into the "std" namespace, so I create my own functions and place them into my own namespace, say... "myLib". In my own version of this library, I also use some of those key function names, such as cin and cout. Now, I could actually mix and match between both versions of cout, by directly using the namespace with it. For example std::cout would call cout in the "std" library, whereas myLib::cout would call mine.

That being said, as long as you know ahead of time that you'll never use a duplicated function name in your program from a different namespace, putting the "using namespace" global is fine, and saves some typing. If you get to where you could have multiple functions with the same name, but in different namespaces, such as what MrRage said, putting a global "using namespace" would no longer work, as the compiler wouldn't know from which namespace your calling the function from.

Scorp

Share this post


Link to post
Share on other sites
Just as an aside, the C++ specification says that main, and main alone, will return zero by default. So the following is a perfectly functioning program:
int main()
{
}

Share this post


Link to post
Share on other sites
Great!!
I had another question or two. What is the ternary operator look like and what is its syntax and use? Is this the symbol ->, if not, what symbol is this?
I've seen it used like: aChar[25] -> strlen(aChar)

Thanks!!

Share this post


Link to post
Share on other sites
That's... I have no idea what you're talking about. A ternary operator is an operator that takes three things. There's only one in C++, and it looks like this:

expr1 ? expr2 : expr3

If expr1 evaluates to true, it'll execute expr2, if not, it'll execute expr3.

The -> operator is a binary operator, which is equivalent of (*pointer).identifier. For example:

struct Me
{
void speak() { std::cout << "Hi!" << std::endl; }
};

int main()
{
Me * me = new Me;
me->speak(); // same as (*me).speak()
}


Finally, I doubt you've actually seen it used like that before, because even though I think it's possible, it's the most screwed up thing I've ever seen. :D. You'd have to write your own class and have all the implicit casts and it'd be a right mess.

I hope this clears up any confusion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!