What happens if I include iostream or any other header file twice in my file?
I know the compiler does not throw error.
Will the code gets added twice or what happens internally?
What actually happens when we include a header file ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Include guard prevents the content of the file from being actually seen twice by the compiler.
Include guard is basically a set of preprocessor’s conditional directives at the beginning and end of a header file:
Now if you include the file twice then first time round macro
SOME_STRING_His not defined and hence the contents of the file is processed and seen by the compiler. However, since the first thing after#ifdefis#define,SOME_STRING_His defined and the next time round the header file’s content is not seen by the compiler.To avoid collisions the name of the macro used in the include guard is made dependent on the name of the header file.