• Welcome to Valhalla Legends Archive.
 

Well...Don't be to hard on me.

Started by Bob, August 10, 2003, 01:09 PM

Previous topic - Next topic

Bob

This is going to make me look really newb, but I really don't know anything about programming and I want to learn C++. Not to make bots or whatever, but b/c I want to learn, I want to be good at it. But I have a question I am just starting out and in the Hello world program as I am sure that all of you have seen at least once...

#include <iostream.h>

int main()
{
    cout << "Hello World! " << endl;

return 0;

}
What would be the difference between using int main() or void main()? Would it change the way the program works, Can someone please explain it? Btw, Yes I know I am a newb, but we all have to learn some time.

Eibro

#1
Assuming your compile compiles void main(), no, it wouldn't change how the program works. However, some compilers won't compile void main() because it's non-standard. main() returns and int, that's just how it is. If you see otherwise (books, internet, etc.) just remember that it's wrong.
Eibro of Yeti Lovers.

Bob


K

#3
just as a good habit, you should use #include <iostream> instead of #include <iostream.h>.

if you try using iostream.h on newer compilers you will get a warning that it's depreciated, or it won't even be found.  The difference is that the iostream header defines everything inside the std namespace.  So your code would look like this:


#include <iostream>

using std::cout; // this means you can type cout in your program
                         // instead of std::cout every time you want to use it.
using std::endl; // or simply "using namespace std" to include the entire namespace

int main()
{
   cout << "Hello World! " << endl;

   return 0;
}


The reason main returns an int is pretty easy to explain.   Suppose you call a function that returns a variable that indicates whether it succeeded or failed. Your program is like a function to the operating system, with 0 (usually) meaning no errors. You can return different values to help the user or the operating system know what happened when running your program.

Hope that helped.

Bob

I understood what is under the code, but to have it known I have no perevious programming experience in C++ let alone hardly any other programming language, So with that said I did not really understand above that, but if you are willing to explain more thoroughly,if possible, then I am willing to try to understand. Thank you for your patience so far.

Eibro

In C++, you can declare and use namespaces.
Namespaces help avoid naming conflicts. If you put all code pertaining to, say, window creation in namespace Window. Domeone else can then come along later and not worry about conflicting names or identifiers. You could have a function, Create() inside namespace Window, and another Create() function in another namespace. Example:
namespace Window
{
  bool Create(const char* name)
  {
     // function body here
  }

  // more functions, etc.
}
That would define the Create function inside namespace Window. To use the Create() function later in your code, you have to do at least one of the following:




1) using namespace Window; This brings all contents of namespace Window into scope.  You can then use anything (function, variable, etc.) within namespace Window by simply using the name it was declared with within that namespace. Example:using namespace Window;
bool Result = Create("MyWindow"); // Calls Window::Create(...)





2) You can bring only portions of a namespace into scope by using the using keyword. An example which would bring the Window::Create() function into scope:using Window::Create;
bool Result = Create("MyWindow"); // Calls Window::Create(...)





3) You can explicitly scope to the namespace each time you want to use an identifier contained within that namespace. bool Result = Window::Create("MyWindow"); // Calls Window::Create(...) [obviously]




With that, the C++ standard library is declared in namespace std. Therefore, to use any functions or identifiers in the standard library (cout, cin, endl) you need to scope to namespace std.
Eibro of Yeti Lovers.

Banana fanna fo fanna


Raven

+1 to everyone who responded to Bob's question. This is more like it should be; none of those 1337 dUd3z saying "OMG HAHAHA n00b!". As long as people are willing to admit they don't know something and courteously ask for help, help should always be provided for them. :)

Camel

+1 to Raven for posting once in the "proper way to post" thread instead of posting in all the others about how much they suck.
>.<

Raven

Quote from: Camel on August 13, 2003, 08:16 PM
+1 to Raven for posting once in the "proper way to post" thread instead of posting in all the others about how much they suck.
>.<

Wha? ;D

Vamp

If void Main() and int main() are the same, then why even use void main(), and how did void main() even like, start being used?

Adron

Quote from: Vamp on August 22, 2003, 08:59 PM
If void Main() and int main() are the same, then why even use void main(), and how did void main() even like, start being used?

Void main saves you "having" to return a value from main. It saves you one line of typing ;)

Hazard

#12
Quote from: Adron on August 23, 2003, 08:18 AM

Void main saves you "having" to return a value from main. It saves you one line of typing ;)

Thank God for shortcuts.

!~!HaZaRD!~!

"Courage is being scared to death - but saddling up anyway." --John Wayne

Yoni

void main() is nonstandard though, so people who use it are violent criminals. You should use int main() only.

Adron

Boycott proper C++ until they allow void main! They have no right to force us to return a value from main! :P