+ 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.
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.
+ 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! đ
+ 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.