Including Same file twice

Hi,

What will happen if we include same file twice in a project.
How comiler and Preprocessor handle it.

Fro Example

1
2
3
4
5
6
7
#include <iostream.h>
#include <iostream.h>

void main()
{
    //some code
}
I would guess that those files will have include guards to prevent multiple inclusions. For example:

1
2
3
4
5
6
#ifndef HEADER_HPP
#define HEADER_HPP

// header stuff goes here

#endif 


If the header has already been, included, the HEADER_HPP macro is already defined so the file is excluded. Many modern compilers also allow you to achieve this with a single line:
#pragma once
(but this might not work on some compilers).

EDIT: I just checked my copy of iostream with MinGW and it did indeed have include guards. So in answer to your question, its content would only be put in the preprocessor output once.
Last edited on
I guess it would depend on the compiler.
In my MS VC++ Express 2010, I (unintentionally) added the time.h file twice, but it worked perfectly.

And for that matter, I checked for iostream in my compiler, and again, it worked perfectly.
Topic archived. No new replies allowed.