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.
Pooja patil said:
4 years ago
By using static we can declare the variable which can use in only this file so other files cannot access.
(1)
Shalvi Rathi said:
9 years ago
We cannot use a non static variable in a static method, hence if we define a method static and the global variable non static, then can't we implement what is referred to in the question?
Colin said:
9 years 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.
Nikith said:
10 years 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.
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?
Knight said:
1 decade ago
Static keyword is use to bind that variable locally, and retains its value in between the function call.
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.
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.
Tamizharasi said:
1 decade ago
What about using static keyword? I think we can achieve it.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers