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 1 of 2.
Colin said:
1 decade ago
By declaring a variable with the same name inside a function will also provide a mechanism.
int i=3;
void fred()
{
int i=6;
printf("%d", i); // will exclude the global variable and display 6
}
So, this is a mechanism that has not made the global variable available to a function.
int i=3;
void fred()
{
int i=6;
printf("%d", i); // will exclude the global variable and display 6
}
So, this is a mechanism that has not made the global variable available to a function.
(1)
Pooja patil said:
6 years ago
By using static we can declare the variable which can use in only this file so other files cannot access.
(1)
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.
Jeremy said:
1 decade ago
This is so incredibly wrong. Look at Ravinder's remark. Also, the explanation contradicts the answer. "You cannot do this; the only way you can do this is ..."; I was thinking something like what Ravinder was
Tamizharasi said:
1 decade ago
What about using static keyword? I think we can achieve it.
Rupinderjit said:
1 decade ago
If we use extern with global variable, then its scope is only limited to its source file in which it is declared.
Debendra Kumar Kar said:
1 decade ago
The answer which has been given is wrong if we will use multiple files.
Because to avoid the access of global variable we can put static keyword before that.
Because to avoid the access of global variable we can put static keyword before that.
Knight said:
1 decade ago
Static keyword is use to bind that variable locally, and retains its value in between the function call.
Akash said:
1 decade ago
Static keyword is use to bind that variable locally, and retains its value in between the function call. Then how can it help?
Nikith said:
1 decade ago
What @Ravinder said above is correct explanations. If we declare global variable under some function it gives compilation error since compilation is done in top down manner.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers