|
INTERVIEW QUESTIONS
C
VARIABLES IN C
DETAILS
Question: Is it acceptable to declare/define a variable in a C header?
Answer: A global variable that must be accessed from more than one file can and should be declared in a header file. In addition, such a variable must be defined in one source file.
Variables should not be defined in header files, because the header file can be included in multiple source files, which would cause multiple definitions of the variable. The ANSI C standard will allow multiple external definitions, provided that there is only one initialization. But because there’s really no advantage to using this feature, it’s probably best to avoid it and maintain a higher level of portability.
“Global” variables that do not have to be accessed from more than one file should be declared static and should not appear in a header file.
|
|
|
Category |
Variables in C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8718 users |
Added on |
10/22/2009 |
Views |
73412 |
Rate it! |
|
|
Question:
Is it acceptable to declare/define a variable in a C header?
Answer:
A global variable that must be accessed from more than one file can and should be declared in a header file. In addition, such a variable must be defined in one source file.
Variables should not be defined in header files, because the header file can be included in multiple source files, which would cause multiple definitions of the variable. The ANSI C standard will allow multiple external definitions, provided that there is only one initialization. But because there’s really no advantage to using this feature, it’s probably best to avoid it and maintain a higher level of portability.
“Global” variables that do not have to be accessed from more than one file should be declared static and should not appear in a header file. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
What is the difference between declaring a variable and defining a variable?
|
View Answer
|
|
Can static variables be declared in a header file?
|
View Answer
|
|
What is the benefit of using const for declaring constants?
|
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
|
|
What is storage class and what are storage variable ?
|
View Answer
|
|
What are the advantages of auto variables?
|
View Answer
|
|
Differentiate between an internal static and external static variable?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Variables in C Interview Questions & Answers - Exam Mode /
Learning Mode
|