Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic question on c++ header file inclusion?

What are the differences between below 3 programs ?. Is <iostream> a header file or C++ standard library ?

1.

#include<iostream>
using namespace std;

int main()
{
        return 0;
}

2.

#include<iostream>

int main()
{
        return 0;
}

3.

#include<iostream.h>

int main()
{
        return 0;
}

Thanks in advance.

like image 228
siva Avatar asked Apr 16 '10 12:04

siva


3 Answers

As far as the program that is produced, there is zero difference - since nothing in the iostream library is referenced by the program, none of the library will be compiled in by any intelligent compiler.

When you #include <iostream>, you're including the header file with declarations for the iostream standard library. Using #include <iostream.h> does essentially the same as #include <iostream>, except that it defines all of the library's names within the global namespace as opposed to being in std:: - it's there for reverse-compatibility with programs that used the original version of iostream which didn't drop the .h. (The <iostream.h> versions also often don't support wide characters, but only standard char's.)

using namespace std; means that the default namespace for name references in the current file will be std, which is the namespace used by most standard library functions. While it means you don't have to prefix all of the standard library calls with std::, it also means that you have to be careful not to define anything that overlaps with standard library names.

like image 73
Amber Avatar answered Oct 21 '22 03:10

Amber


There is an error in no 3 problem with header file

The header iostream.h is a non-standard header and does not exist on all platforms. As a matter of fact it does not exist on my system (using g++ and the GNU libstdc++). So any code using it would simply not compile on my system.

The iostream.h header used to be common before C++ was first standardized in 1998. But since the 98 standard used <iostream> instead of <iostream.h>, the latter has fallen out of favor (being non-standard and all) and is no longer supported on all platforms. Code that uses it should be considered non-standard legacy code and is not portable. Books that teach it should be considered outdated and avoided.�

like image 37
Saikat Kundu Avatar answered Oct 21 '22 05:10

Saikat Kundu


iostream is a header file that provides declarations and prototypes that are an interface to part of the C++ standard library.

There's is, on your system, a file called "iostream" (no extension), the contents of which are copied and pasted (with recursive processing of #includes) at the point where you write #include <iostream>.

#include directives always pull in the contents of header files, they never add "libraries". Header files often contain declarations and prototypes that are an interface to a library, but the actual libraries themselves are attached to your program by the linker, not the compiler. When linking a C++ program, the linker will automatically attach the C++ standard library unless you tell it not to, so you don't have to worry about that.

Similarly, the using namespace std statement does not do the work of attaching the library. This statement only makes it so that you can write, for example, cout or string instead of qualifying them as std::cout and std::string. This works for any namespace, but is usually discouraged.

For the three examples you gave, they all give you the definitions and prototypes you need to use the iostream portion of the C++ standard library, but (2) is preferred, (1) is acceptable, and (3) is deprecated. (2) gives the additional convenience of being able to omit the std:: prefix (at the cost of reducing the variable names available for you to use yourself), and (3) includes a different file called "iostream.h" instead of "iostream", which is usually the same thing, but the file with the .h is a relic of pre-standard C++ and so may not be supported in future compilers.

like image 22
Tyler McHenry Avatar answered Oct 21 '22 04:10

Tyler McHenry