-
December 1st, 2009, 03:30 PM
#1
Could someone please end some slight confusion?
Hey guys sorry for the another post so fast.
I have been learning C++ for a few days now and i have come across this:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return EXIT_SUCCESS;
}
but the way i have learned is this:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello, World!" <<endl <<endl;
system("PAUSE>NUL");
return 0;
}
can someone please explain, Thanks!
-
December 1st, 2009, 03:39 PM
#2
Re: Could someone please end some slight confusion?
What do you need explained? Really, they are both valid programs. The first returns immediately after printing "Hello, World!" (which exits the program). The second executes the command "PAUSE" after printing "Hello, World!"
Viggy
-
December 1st, 2009, 03:48 PM
#3
Re: Could someone please end some slight confusion?
One pauses execution and lets you see the output, the other exits immediately so you'll see nothing (unless the IDE pauses it for you, which is usually the case)
-
December 1st, 2009, 03:52 PM
#4
Re: Could someone please end some slight confusion?
but what about the std:: is that because in the other program i used using namespace std ??
-
December 1st, 2009, 04:05 PM
#5
Re: Could someone please end some slight confusion?
Originally Posted by JonnySnip3r
but what about the std:: is that because in the other program i used using namespace std ??
first form (using declaration) has the control over member introductions,
where as the latter does not (using directive).
Code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return EXIT_SUCCESS;
}
but the way i have learned is this:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello, World!" <<endl <<endl;
system("PAUSE>NUL");
return 0;
}
Neither of these programs are properly written
-
December 1st, 2009, 04:11 PM
#6
Re: Could someone please end some slight confusion?
Originally Posted by ninja9578
One pauses execution and lets you see the output, the other exits immediately so you'll see nothing (unless the IDE pauses it for you, which is usually the case)
Not necessarily.
The output will reamin in the console if the program is run at the command prompt
-
December 1st, 2009, 04:31 PM
#7
Re: Could someone please end some slight confusion?
Originally Posted by JonnySnip3r
but what about the std:: is that because in the other program i used using namespace std ??
Essentially yes. There are some slight differences between the two approaches (you can read about argument-dependent lookup if you want), but for most purposes you can consider explicit namespace specification versus a "using namespace" statement to be equivalent.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|