+ 2

What is scope of a variable? How are variables scoped in C?

28th Dec 2019, 6:45 AM
Somasuntharam Harsana
Somasuntharam Harsana - avatar
2 odpowiedzi
+ 3
Just keep in mind that in general there are two types of scopes- local and global. A local variable can be only accessed within the block in which it is declared but a global variable can be accessed anywhere inside the program. Eg- #include<stdio.h> int x; int main(){ int y; } //The variable "y" has scope limited to main() and can not be accessed outside the main() whereas the variable "x" has a global scope and can be accessed anywhere throughout the program.
28th Dec 2019, 7:01 AM
Avinesh
Avinesh - avatar
+ 1
Scope of a variable means a variable boundary where the variable exists or available. It can be global scope or local scope. Local scope means the variable available only in the function where that is declared. Ex: int fun(int i) { char c='a' ; .... ... } Here, i, c variables has local scope. So outside of fun function, it is not exists, or undefined. Global scope means the variables available through out the program across all the functions. Ex: Macros. Ex: #include <stdio.h > int i=10; char c=' a' ; Int main () { printf ("%d", i) ; .. fun(c) ; } void fun() { printf ("%c", c) ; ... } Here i, c are global variable so available to owl program. Hoping it helps you... Edit: Storage class of a variables is determined by 4 access specifiers in c : those are Static : it is a local variable retains its value even throughout different function calls. extern : it is global scope. auto : local scope limited to function where it is declared. Register : variable are stored in registers instead of stacks. So vari
28th Dec 2019, 7:08 AM
Jayakrishna 🇮🇳