C Programming - C Preprocessor - Discussion


Will it result in to an error if a header file is included twice?

[A]. Yes
[B]. No
[C]. It is compiler dependent.

Answer: Option C


Unless the header file has taken care to ensure that if already included it doesn't get included again.

Turbo C, GCC compilers would take care of these problems, generate no error.

Tim said: (Dec 7, 2010)  
Your answer depends on the CONTENTS of the header file. For example, a header file that is EMPTY can be included as many times as you like!

Chetana said: (Mar 18, 2011)  
Dont you think your explaination & Answer are differ. Please reply.

Pruthvi said: (Sep 17, 2011)  
Can any one tel me what will b the error if we include an header file twice. ? as the linker will link to any of the headerfiles function then how the error comes. ?

Saravanan A said: (Dec 1, 2011)  
Yes its very much dependent on the content. If only extern function declarations and extern variable declarations are there it will not be a issue. Only if some types are defined like structure - redefinition error will come. Or if a variable if defined which is a rare case though.

Onkar said: (Jan 25, 2012)  
I didn't get it please explain it again in simple words.

George said: (Jan 8, 2013)  
It depends in the header file. If there are inclusion guards, then it should be OK.


#ifndef __STDIO_H__
#define __STDIO_H__

Once the header is a called a second time, #ifndef __STDIO_H__ will evaluate to FALSE.

Gurunath said: (Dec 18, 2014)  
We don't understand clearly.

Vjay said: (Jun 11, 2017)  
#define calc(a,b) (a*b)/(a-b)
Int a=20,b=10;
Printf("%d", calc(a+4,b-2));

What will be the answer? Please tell me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.