+ 5

Can anyone tell me what is the 'using namespace' and 'iostream' is used for?

I'm new to this programming language so I need some help to figure it out.

16th Nov 2017, 8:35 AM
Jayprakash Malik
Jayprakash Malik - avatar
5 Answers
+ 2
Hello Jayprakash, #include <iostream> make the data and function definitions for input and output available to your program. This is a basic library and there is always the possibility that someone might use the same names, so in order to prevent variable and function name duplication, code can be grouped into a 'namespace'. So you don't really need to use the namespace statement ... BUT ... if you don't include the 'using namespace std;' then you would need to preface each variable and function in the std.h library with the scope operator std:: to access it. For example, if you don't use add 'using namespace std;', to output data you would need to do the following: std::cout << "print_me" << std::endl; By adding 'using namespace std;' at the top of your program, all of the definitions grouped using the std namespace can be accessed directly.
18th Nov 2017, 6:15 PM
Michael Loutris
Michael Loutris - avatar
+ 9
"#include iostream" tells the compiler to... include "iostream" in it. Basically, iostream is a header file with predefined functions required for input and output, including 'cout' and 'cin'. "#include <iostream>" just tells the computer to use the iostream header file. Let's have a simple example: #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; } //Hello World! Meanwhile, without the first line: using namespace std; int main(){ cout << "Hello World!"; return 0; } //Error: 'cout' was not defined in this scope In summary, iostream has predefined functions for input and output including cout and cin and it is vitally important if you need to use either of those. Hope this incredibly lengthy explanation helped! 😉
16th Nov 2017, 8:52 AM
blackcat1111
blackcat1111 - avatar
+ 1
"#include <iostream>" says to compiler what header file is 'iostream". Its basic header file. "using namespace std" says to compiler what spaces is standard.
17th Nov 2017, 7:12 AM
MrMoki23