GeekInterview.com
Series: Subject: Topic:
Question: 813 of 828

†Can static variables be declared in a header file?

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. †
Asked by: Interview Candidate | Asked on: May 4th, 2005
Showing Answers 1 - 6 of 6 Answers
Jatin Bhateja

Answered On : May 4th, 2005

Can we declate static variable as global.Will there be any purpose of making such decleration.

Yes  1 User has rated as useful.
  
Login to rate this answer.
paulsonpaulc

Answered On : Apr 20th, 2006

View all answers by paulsonpaulc

Yes there is difference between declaring a static variable as global and local. If it is local, it can be accessed only in the function where it's declared. But if it is global, all functions can access it. But, what ever be the case, its value will be retained between functions.

Yes  1 User has rated as useful.
  
Login to rate this answer.
paulson paul chambakottukudyil

Answered On : Apr 23rd, 2006

A global variable must be declared as static if you want to restrict its access within that file.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Ekta Goel

Answered On : May 3rd, 2007

View all answers by Ekta Goel

If you make a global variable as static, it does not mean it can not access outside that class.With tha name of the class, static global variable can be accessed.

Yes  1 User has rated as useful.
  
Login to rate this answer.
vg_220

Answered On : Aug 14th, 2007

View all answers by vg_220

We are talking C. Isn't it? C doesn't have class construct so please think the 'C' way ! According me, static declaration in a header file should not generally be useful. Can anyone suggest a good use of it?

Yes  1 User has rated as useful.
  
Login to rate this answer.
sssss

Answered On : Nov 11th, 2007

In CThe scope of static variable is file. ie., if declared the variable is accessible accross the file.When declared the vairable in a header then its exposed to where ever its included.  Every file will have a local copy.  You need to extern the variable, ie need to import into other file file externing it.The suggested way to use is declare it inside a implementation file, it behaves like a private.  If declated it inside a header file it will be made public and exposed to exernal files when used to with extern declaration. The good practice is declare globals in  a header and defile the extern.  so when ever the variable being included it will be imported to that file.  or ther other way is variable can be exposed by using extern.

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.