Scope rules in programming C
A scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed. There are 3 places where variables can be declared in Language C:
- Inside a function or a code block is called a local variable.
- Outside all functions and called global variables .
- In the definition of functions, parameters are called formal - formal parameters.
Let us now find out what local variables and global variables and formal parameters are.
Local variable in C
Local variables are declared inside a function or declared inside a code block. They are used by commands in that function or code block. Local variables are not used outside of the function. Below is an example of using local variables. Here variables a, b and c are used in main ():
#include int main () { /* Khai bao bien cuc bo */ int a , b ; int c ; /* khoi tao thuc su */ a = 10 ; b = 20 ; c = a + b ; printf ( "Gia tri cua a = %d, b = %d va c = %dn" , a , b , c ); return 0 ; }
Global variable in C
Global variables are defined outside a function, usually the first part of the program. Global variables can contain values during program run and can be accessed by any function defined in the program.
A global variable can be accessed by any function. That means global variables are used throughout the program after it declares. Here is an example to illustrate local and global variables:
#include /* Khai bao bien toan cuc */ int g ; int main () { /* Khai bao bien cuc bo */ int a , b ; /* khoi tao bien thuc su */ a = 10 ; b = 20 ; g = a + b ; printf ( "Gia tri cua a = %d, b = %d va g = %dn" , a , b , g ); return 0 ; }
A program can have global and local variables with the same name. In that case, the local variable inside the function will be used. Here is an example:
#include /* Khai bao bien toan cuc */ int g = 20 ; int main () { /* khai bao bien cuc bo */ int g = 10 ; printf ( "Gia tri cua g = %dn" , g ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Run the above C program to compile and run to see the results:
Official parameter in C
The function parameter, the official parameter, is treated as a local variable within that function and usually takes precedence over the global variable. Here is an example:
#include /* khai bao bien toan cuc */ int a = 20 ; /* khai bao ham */ int hamtinhtong ( int a , int b ); int main () { /* khai bao bien cuc bo trong ham main */ int a = 15 ; int b = 25 ; int c = 0 ; printf ( "Gia tri cua a trong ham main() = %dn" , a ); c = hamtinhtong ( a , b ); printf ( "Gia tri cua c trong ham main() = %dn" , c ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } /* ham de cong hai so nguyen */ int hamtinhtong ( int a , int b ) { printf ( "Gia tri cua a trong ham hamtinhtong() = %dn" , a ); printf ( "Gia tri cua b trong ham hamtinhtong() = %dn" , b ); return a + b ; }
Run the above C program to compile and run to see the results:
Initialize global variables and local variables
When global variables and local variables are defined, it is not initialized by the program, it must initialize by yourself. Global variables usually initialize automatically by the program when you define them as follows:
Data type Default value 0 char '' float 0 double 0 pointer NULLOften in practice programming you should initialize variable values correctly, otherwise your program may cause unwanted results.
According to Tutorialspoint
Previous article: Function in programming C
Next lesson: Array in Language C
You should read it
May be interested
- Situations should escape quickly in Rules of Survivalwith certain situations in the rules of survival, players need to run away to save lives, without having to calculate too much.
- The main weapons in the Rules Of Survival you should knowweapons included in rules of survival will be the main factor that helps users to attack and defeat opponents quickly.
- How to install the Rules Of Survival game on PCinstructions for installing rules of survival game on az computers, help you play this survival shooter on a larger pc screen. to be able to install the international rules of survival on your computer, you need to install the rules of survival game on your phone and then scan the qr code to install.
- How to fix errors hanging game Rules of Survivalsome bugs when playing rules of survival game on your computer like throwing the game suddenly, hanging the logo and not continuing to load data makes the players quite uncomfortable.
- How to fix errors showing teammates in Rules Of Survivalin squad mode of rules of survival, many faulty gamers display friends 'names, friends' locations are not identical to the names displayed on the screen, making it difficult for gamers.
- How to play the Rules Of Survival game on the computerrules of survival is a multi-player shooter that has survived in recent times. so how to play the game?
- How to play Rules Of Survival when the ball is narrowed into the sea, rivers and lakeswhen the boils in rules of survival narrowed down the river area, it would cause players to fall into danger because this place has little hiding and hiding places.
- Weapons to lower vehicles in Rules of Survivalsome weapons in rules of survival are capable of destroying vehicles, including cars or airplanes, etc.
- How to know the enemy hiding in the house on Rules of Survivalthe houses on rules of survival will become where the opponent hides and attacks you. so how do you know if there are enemies hiding in the house?
- How to create rules (Rules) in Outlookhow to create rules (rules) in outlook. rules in outlook help you manage emails in your mailbox more effectively. it allows you to move mail from one folder to another, attach different flags to classify messages and even help you reply to emails automatically ... etc ...