-5

I have searched this question in Google. I found some related question in stackoverflow.com and quora.com but i am still not clear about this two topics. Everyone says that we use #include<iostream> for input/output operation. Now, we take input using cin and print output using cout that means this two should be defined in #include<iostream>. But without using using namespace stdwe still can't take any input nor can print something on console. So, my questions are-

  1. Where is cin and cout actually declared and defined? Is it in #include<iostream>or in namespace std?
  2. If in #include<iostream>why should we use using namespace std?
  3. If in namespace std why should we use #include<iostream?

After reading some article on the web and watching some videos on YouTube, I'm assuming cout and cin is defined in namespace std and the namespace std doesn't make any sense alone because it is defined in #include<iostream>. That's why we need to use them both. (Just my thought let me know if I am right or not.)

The purpose of this question is to be clear about this two facts. If you can help it would be great.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Rubel Hosen
  • 37
  • 1
  • 1
  • 5
  • 4
    [Don't use `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – NathanOliver Feb 14 '17 at 18:20
  • 1. Both, 2. You shouldn't, 3. Because thats where `std::cout` and `std::cin` come from. – nwp Feb 14 '17 at 18:20
  • 3
    _But without using `using namespace std` we still can't take any input nor can print something on console._ **Wrong**. Use `std::cin`/`std::cout` instead. – Algirdas Preidžius Feb 14 '17 at 18:22
  • 4
    The most help I can give would be to point you to [this list of books](http://stackoverflow.com/a/388282/2069064) – Barry Feb 14 '17 at 18:24
  • @Raindrop7 _"so you don't have to include all the content of the header:"_ Sure?? Or did you mean _use all the content_ (reading _include_ as `#include`). – πάντα ῥεῖ Feb 14 '17 at 18:26

5 Answers5

4

cin and cout are defined in the header iostream and in the namespace std. These concepts are orthogonal. iostream is a file name and std is a namespace used by the source code of that file.

As a way of keeping things organized, c++ provides namespaces. All of the standard library is defined within the namespace called std. Other libraries you might write or include may use their own namespace. For example, you might include a physics library in your project which wants to define the concept of algebraic vectors. By using it's own namespace (let's called if physlib) it can differentiate between it's vector (physlib::vector) and the standard vector (std::vector). Namespaces can also be nested to help organize large projects. For example, time keeping parts of the standard library are in std::chrono and file system related components are in std::filesystem.

The preferred way of using cin and cout is as following. :

#include <iostream>

int main()
{
    std::cout << "Hello, World!\n";
    return 0;
}

The statement using namespace std is simply an instruction to look in the namespace std by default. It allows you to omit the std:: part of using standard library components. It's generally regarded as a bad idea to used using namespace std.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
3

Why should we use #include <iostream>

To bring the standard library's I/O functionality into our program.

while we are using using namespace std?

This allows us to use that functionality without writing std:: each time we do.

This is unrelated to the previous step. Writing only using namespace std does not bring I/O functionality into your program, and writing only #include <iostream> does not allow us to use that functionality without writing its components' names out in full (including the std:: prefix).

  • The #include directive determines what we can use;
  • The using namespace declaration determines how we can use it.

Perfectly fine:

#include <iostream>

int main()
{
   std::cout << "Hello world!\n";
}

Also valid:

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world!\n";
}

Not valid:

int main()
{
   std::cout << "Hello world!\n";
}

And neither is this:

using namespace std;

int main()
{
   std::cout << "Hello world!\n";
}

or this:

using namespace std;

int main()
{
   cout << "Hello world!\n";
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I have assumed throughout that you have misunderstood what `using namespace std` means. – Lightness Races in Orbit Feb 14 '17 at 18:44
  • Well, I knew we use `using namespace std` to reduce our typing which is adding `std::` every time before we write something like `cout` or `cin`. I also knew that we use `namespace` to avoid naming collision. I just wanted to know the relation between `namespace` and the `header file` why we need them both. – Rubel Hosen Feb 14 '17 at 19:03
  • @RubelHosen: Well if you know what `using namespace std` does then I cannot comprehend why you think `#include` is related. – Lightness Races in Orbit Feb 14 '17 at 19:10
  • `@Lightness Races in Orbit` Because I only know what `namespace` does, I only know what `header file` does. I don't know the relationship between them, I don't know why I need them both. I can't make use of `cout` without `std` and `iostream`. I don't know what is stopping me. i don't know if `namespace` is a part of `iostream` and `cout` is defined in `std` nor I know if `iostream` is a part of `std` and `cout` is defined in `iostream`. – Rubel Hosen Feb 14 '17 at 19:42
  • @RubelHosen: Sorry, if you understand what headers do and what `using namespace` does, you already know the two are unrelated. I'm not trying to be difficult - if I can't understand your misconception, I can't help any further to correct it. – Lightness Races in Orbit Feb 14 '17 at 20:17
2

#include <iostrem> tells the compiler to pull in the contents of the header iostream. That header provides, among other things, declarations of the objects std::cin and std::cout. So the basic "Hello, world" program looks like this:

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

Similarly, if you want to use std::vector, you tell the compiler about it with #include <vector>. Same thing for the rest of the standard library: whatever it is that you want to use, find out which header declares it and #include that header.

using namespace std; doesn't define any names for you. It tells the compiler to pretend that any names that have been defined in the namespace std are also defined in the scope where using namespace std; occurs. So that means that instead of writing std::cout you can write cout, and the compiler will figure out that you meant std::cout. Unless, of course, you've written something yourself with the name cout (or any other name that's in std and declared in a header that you've #included), in which case that using declaration makes the use of the name ambiguous. There is no good reason to write using namespace std;. Just use the right names for things: std::cout is clear and unambiguous.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1
  1. Where is cin and cout actually declared and defined? Is it in #include<iostream> or in namespace std?

It's not or. cin and cout are declared in the iostream header file within the namespace std.

  1. If in #include< iostream> why should we use using namespace std?

You shouldn't. Either fully qualify the global variables like std::cin or std::cout. There's a number of reasons why you shouldn't use using namespace std; (at least in header files).

  1. If in namespace std why should we use #include<iostream>?

Because you need the declarations to compile your code.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

iostream is part of the standard library, here is what the "iostream.h" file would look like if you were to implement it yourself:

namespace std{
// code about cin
  extern ostream cin;
// code about cout
  extern ostream cout;
}

(see c++ STL cout source code)

Where is cin and cout actually declared and defined? Is it in #include <iostream> or in namespace std?

So cin/cout are declared and define in the standard library under the namespace std. This means that if you want to use it in your code you can do :

int main()
{
  std::cout << "Hello";
}

If in #include<iostream> why should we use using namespace std?

You don't need using namespace std, you could call std::cout everywhere instead of cout, but you can see how this can make your code verbose and annoying to type. using namespace std in your whole file is actually discouraged as it could have unintended effect, usually you could use only

using std::cout;
using std::cin;

where needed.

The reason the using is needed (or the need for explicitly writing std:cout , is imagine you have the following code:

#include <iostream>
namespace mycoolnamespace
{
  class foo { 
     public:
     foo& operator<< (const std::string& echo)
     {
         // do stuff
     }
  };


  foo cout; // created a variable called cout
}

int main()
{
  cout << "Hello"; 
 // ^^^^ compiler can't know if you meant to use std::cout or mycoolnamespace::cout here!

  // Potential fix:
  std::cout << "Hello"

}

If in namespace std why should we use #include<iostream>?

By default not all function/classes from the standard library are included in your program, otherwise a simple "Hello world" program would give a lot of work to the compiler since it would need to parse all the existing classes/functions in the entire standard library. #include<iostream> tells the compiler that you need the functions/classes available in the standard library's iostream 'file'

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Vincent
  • 444
  • 4
  • 6