Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Scope Rules in Programming C

Explore Scope Rules in Programming C with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers scope rules in programming c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

  1. Inside a function or a code block is called a Local Variable.
  2. Outside all functions and called Global variables .
  3. 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 NULL

Often 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

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.