473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using std::cout

Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.

This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:

#include <iostream>
using std::cout;
using std::endl;

int main() {
cout << "hello, world" << endl;
return 0;
}

or like this:

#include <iostream>

int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :

#include <iostream>

int main() {
cout << "Hello, World << endl;
return 0;
}

My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.

Aug 16 '07 #1
6 60922
Roger wrote:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.

This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:

#include <iostream>
using std::cout;
using std::endl;

int main() {
cout << "hello, world" << endl;
return 0;
}

or like this:

#include <iostream>

int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :

#include <iostream>

int main() {
cout << "Hello, World << endl;
return 0;
}

My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.
std is a namespace. Namespaces exist to prevent names from accidentally
clash with each other. Imagine you work on a project with someone else, and
both you and your colleague define a function named 'doWhatIWant'. If now
you and your colleague define the name in different namespaces, this would
not be a problem. As the standard library contains a lot of names, it
defines most of them in the namespace std.

A note: I think

#include <iostream>
int main() {
std::cout << "Hello, World\n";
return 0;
}

is better. std::endl just sends a newline character to the stream, and then
flushes it.

--
rbh
Aug 16 '07 #2
On Aug 16, 3:34 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
Roger wrote:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.
This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "hello, world" << endl;
return 0;
}
or like this:
#include <iostream>
int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :
#include <iostream>
int main() {
cout << "Hello, World << endl;
return 0;
}
My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.

std is a namespace. Namespaces exist to prevent names from accidentally
clash with each other. Imagine you work on a project with someone else, and
both you and your colleague define a function named 'doWhatIWant'. If now
you and your colleague define the name in different namespaces, this would
not be a problem. As the standard library contains a lot of names, it
defines most of them in the namespace std.

A note: I think

#include <iostream>
int main() {
std::cout << "Hello, World\n";
return 0;

}

is better. std::endl just sends a newline character to the stream, and then
flushes it.

--
rbh
So by defining it after the #include <iostream>, I won't have to use
std::cout?

Aug 16 '07 #3
So by defining it after the #include <iostream>, I won't have to use
std::cout?
Yes.
You could also, write "using namespace std", to register the complete
namespace.

#include <iostream>
using namespace std;

int main() {
cout << "hello, world" << endl;
return 0;
}

Aug 17 '07 #4
On Aug 16, 5:28 pm, kamistr...@gmx.de wrote:
So by defining it after the #include <iostream>, I won't have to use
std::cout?

Yes.
You could also, write "using namespace std", to register the complete
namespace.

#include <iostream>
using namespace std;

int main() {
cout << "hello, world" << endl;
return 0;

}
Wow! Thanks for the tip. I didn't know that. :-)

Aug 17 '07 #5
Roger wrote:
On Aug 16, 5:28 pm, kamistr...@gmx.de wrote:
>>So by defining it after the #include <iostream>, I won't have to use
std::cout?
Yes.
You could also, write "using namespace std", to register the complete
namespace.

Wow! Thanks for the tip. I didn't know that. :-)
However, a good piece of advice, if you're going to do that is to never,
NEVER, *NEVER* place "using namespace std" in a header file. It can
mess up any other headers that follow yours.

If you feel like using "using namespace std;", place it in your .cpp
source file, after all includes.
Aug 17 '07 #6
Hi!

red floyd schrieb:
However, a good piece of advice, if you're going to do that is to never,
NEVER, *NEVER* place "using namespace std" in a header file. It can
mess up any other headers that follow yours.
:D Yes! A friend of mine recently got bitten by it. It made
std::ifstream and boost::filesystem::ifstream mix up.
If you feel like using "using namespace std;", place it in your .cpp
source file, after all includes.
Yes! *AFTER ALL INCLUDES*

Frank
Aug 17 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3942
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
1
3379
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the...
2
3283
by: Pmb | last post by:
I've noticed a lot of people preferring std::cout over cout. May I ask why you prefer one over the other? thanks Pmb
1
3537
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
6
3088
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int...
7
2205
by: patrick | last post by:
Why do some code listings for learning C++ have the entire namespace std being used while others just specify the parts they want to use?
5
2518
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
58
4773
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
2
2290
by: donthomasino | last post by:
i get error c2886( when i use using std::cout; using std::string;) and when i change to (using namespace std; using namespace std;) i got error c2059 #include <iostream> #include <string>...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.