CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How can you avoid including a header more than once?
Category C Interview Questions
Rating (4.7) By 25 users
Added on 10/22/2004
Views 3641
Rate it!
Answers:

One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define
preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. You can use the conditional preprocessor directive named #ifndef to check whether that symbolic name has already been assigned. If it is assigned, you should not include the header, because it has already been preprocessed. If it is not defined, you should define it to avoid any further inclusions of the header. The following header illustrates this technique:

#ifndef _FILENAME_H
#define _FILENAME_H
#define VER_NUM ?1.00.00?
#define REL_DATE ?08/01/94?
#if _ _WINDOWS_ _
#define OS_VER ?WINDOWS?
#else
#define OS_VER ?DOS?
#endif
#endif

When the preprocessor encounters this header, it first checks to see whether _FILENAME_H has been defined. If it hasn?t been defined, the header has not been included yet, and the _FILENAME_H symbolic name is defined. Then, the rest of the header is parsed until the last #endif is encountered, signaling the end of the conditional #ifndef _FILENAME_H statement. Substitute the actual name of the header file for ?FILENAME? in the preceding example to make it applicable for your programs.



The method explained above is absolutely correct. But there is another way to solve this multiple inclusion of headers.

This is done by using #pragma ONCE.

Here is the example for this.
// In a.h (header file save the below lines
#pragma once
typedef int INTEGER;

// In abc.c include the following code.

#include <stdio.h>
#include "a.h"
#include "a.h"

INTEGER age = 24;

int main (int argc, const char * argv[]) {

printf("age = %d ", age);
system("pause");
return 0;
}

Here i am including the same header twice. This should give me an error. Whereas on using the #pragma ONCE, it wont give. If we comment #pragma ONCE statement, it will give a compile time error " redefinition of typedef 'INTEGER' ".



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Can a file other than a .h file be included with #include?
View Answer
What is the benefit of using #define to declare a constant?
View Answer
What is the benefit of using an enum rather than a #define constant?
View Answer
How are portions of a program disabled in demo versions?
View Answer
Is it better to use a macro or a function?
View Answer
What is the difference between #include <file> and #include ?file??
View Answer
Can you define which header file to include at compile time?
View Answer
Can include files be nested?
View Answer
How many levels deep can include files be nested?
View Answer
How can type-insensitive macros be created?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1031/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.83