Valhalla Legends Forums Archive | C/C++ Programming | Well...Don't be to hard on me.

AuthorMessageTime
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.
August 10, 2003, 6:09 PM
Eibro
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.
August 10, 2003, 6:19 PM
Bob
Thank you very much.
August 10, 2003, 6:23 PM
K
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:

[code]
#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;
}
[/code]

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.
August 10, 2003, 7:01 PM
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.
August 10, 2003, 10:33 PM
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:
[code]namespace Window
{
bool Create(const char* name)
{
// function body here
}

// more functions, etc.
}[/code]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:

[hr]

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:[code]using namespace Window;
bool Result = Create("MyWindow"); // Calls Window::Create(...)[/code]

[hr]

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:[code]using Window::Create;
bool Result = Create("MyWindow"); // Calls Window::Create(...)[/code]

[hr]

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

[hr]

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.
August 10, 2003, 11:04 PM
St0rm.iD
Bob: well asked question. +1
August 13, 2003, 2:52 PM
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. :)
August 13, 2003, 11:32 PM
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.
>.<
August 14, 2003, 1:16 AM
Raven
[quote author=Camel link=board=5;threadid=2274;start=0#msg18027 date=1060823771]
+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.
>.<
[/quote]

Wha? ;D
August 15, 2003, 8:50 PM
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?
August 23, 2003, 1:59 AM
Adron
[quote author=Vamp link=board=5;threadid=2274;start=0#msg18892 date=1061603969]
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?
[/quote]

Void main saves you "having" to return a value from main. It saves you one line of typing ;)
August 23, 2003, 1:18 PM
DrivE
[quote author=Adron link=board=5;threadid=2274;start=0#msg18913 date=1061644703]

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

Thank God for shortcuts.

!~!HaZaRD!~!
August 23, 2003, 1:23 PM
Yoni
void main() is nonstandard though, so people who use it are violent criminals. You should use int main() only.
August 23, 2003, 1:32 PM
Adron
Boycott proper C++ until they allow void main! They have no right to force us to return a value from main! :P
August 23, 2003, 2:43 PM
Vamp
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?
August 23, 2003, 2:59 PM
Yoni
[quote author=Vamp link=board=5;threadid=2274;start=15#msg18924 date=1061650799]
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?
[/quote]

That just means it has no parameters. This is perfectly legal.
Some valid mains:

[code]int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char *argv[], char *envp[])[/code]

The first two are essentially the same, and the last two are for when you need to get commandline/environment info.
August 23, 2003, 4:07 PM
Eibro
[quote author=Yoni link=board=5;threadid=2274;start=15#msg18930 date=1061654828]
[quote author=Vamp link=board=5;threadid=2274;start=15#msg18924 date=1061650799]
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?
[/quote]

That just means it has no parameters. This is perfectly legal.
Some valid mains:

[code]int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char *argv[], char *envp[])[/code]

The first two are essentially the same, and the last two are for when you need to get commandline/environment info.
[/quote]Also, the last one is non-standard, iirc.
August 24, 2003, 12:37 AM
Yoni
[quote author=Eibro link=board=5;threadid=2274;start=15#msg18961 date=1061685432]
Also, the last one is non-standard, iirc.
[/quote]
Eibro is correct. envp is not standard as I thought, but rather a very common extension. +1!

The standard way to get environment variables is getenv() in stdlib.h.
August 24, 2003, 1:55 AM
Bob
Well It's Bob again ;D Long time no post, But I've learned a little bit more and I was wondering if anyone could give me some sort of a "quiz program" to test what I have learned nothing to difficult--Thanks--

When I say quiz program I mean something for me to make to test my knowledge thanks again--
August 26, 2003, 2:06 PM
Adron
Make the command line application Drag that implements this function.

Start the program with a file name (wildcards allowed) as argument.

[code]
C:\Music>Drag *kylie*mp3
[/code]

The mouse cursor becomes a "drag file" cursor, and you can drop the files into a msn chat window to send them to someone.

You will find the first file name in argv[1]. Allow multiple files to be dragged at once.

Post the source code here.
August 26, 2003, 3:26 PM
Dricoust
Hi,
main's return value tells the os on how the application ran,
return 0; Everything ran correct.
return 1; Dident run correct.

Using void main is acceptable by all means. Although as i have acknowledged, when void is used the os still requires the status of what happened, and since no return. It always returns 0;.
Usually when using int main() you can return the value (Ending the program) at you're own choice. ie

#include <iostream>
using namespace std;

int main()
{
char cmd[5];
cout << "Enter a number between one and 5";
cin >> cmd;
if(atoi(cmd) > 5) {
return 1; // Assuming the value is critical to be
// between 1 & 5.
}
else { return 0; }
}

As to my understanding.
And you are certainly not a 'newb' by any means. Damn i had trouble with JavaScript first time i used it.
Now i have my own chat server, using VB as script =]

Refrences:
C++ From The Ground Up
MFC Programming With Visual C++.NET 2003
C++ The Complete Refrence Edition1 - 5.
Programming With DirectX9a.
November 6, 2003, 12:42 PM
St0rm.iD
This thread is pretty stale.
November 6, 2003, 11:31 PM

Search