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


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Can static variables be declared in a header file?
Category C Interview Questions
Rating (4.1) By 53 users
Added on 10/22/2004
Views 13718
Rate it!
Answers:

You can?t declare a static variable without defining it as well (this is because the storage class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.



Static variables can be declared in a header file. However, their definition should also be provided in the same header file. But , doing this makes the static variable as a private copy of the header file ie it cannot be used elsewhere. In normal cases, this is not intended from a header file. It is generally a ad idea to use a static variable in a header file.



 Posted by: Manik Sidana    

Contact Manik Sidana  Contact Manik Sidana

Static (becoming global in header files) variables can be defined in .h file. However you should make sure that the header is accepted only once by including #ifndef and #ifdef. By this the compiler will not raise errors saying redefinition of same variable.

#ifndef __static_var_definitions__
#define __static_var_definitions__

static int gblCounter;

#endif



 Posted by: Yogendra Namjoshi    

Contact Yogendra Namjoshi  Contact Yogendra Namjoshi


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
What is the benefit of using const for declaring constants?
View Answer
What is the easiest sorting method to use?
View Answer
What is the quickest sorting method to use?
View Answer
How can I sort things that are too large to bring into memory?
View Answer
What is the easiest searching method to use?
View Answer
What is the quickest searching method to use?
View Answer
What is hashing?
View Answer
How can I sort a linked list?
View Answer
How can I search for data in a linked list?
View Answer
How do you redirect a standard stream?
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/1050/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.78