C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Yes / No Questions (Q.No. 3)
3.
Global variable are available to all functions. Does there exist a mechanism by way of which it available to some and not to others.
Answer: Option
Explanation:
The only way this can be achieved is to define the variable locally in main() instead of defining it globally and then passing it to the functions which need it.
Discussion:
11 comments Page 2 of 2.
Ravinder said:
2 decades ago
Yes, you can do achieve this by declaring the variable after the functions from which you want to hide from. Take the following example:
void first();
void second();
void first()
{
printf("%d", i);
}
int i=3;
void second()
{
printf("%d", i);
}
The variable 'i' is accessible only to function "second". I used gcc.
void first();
void second();
void first()
{
printf("%d", i);
}
int i=3;
void second()
{
printf("%d", i);
}
The variable 'i' is accessible only to function "second". I used gcc.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers