Skip to main content

Scope of Variable in C

Scope of Variable in C

In this tutorial, We will learn about scoping rule in C programming language.

Introduction

A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration. There is three type of variable with respect to the scope of the variable.
  • Local Variables
  • Global Variable
  • Formal Parameter

Local Variable

Variables that are declared within the function block and can be used only within the function is called local variables.
 #include <stdio.h>  
 int main ()  
 {  
   int x,y,z; // x, y, z local variable definition   
   // actual initialization   
   x = 20;  
   y = 30;  
   z = x + y;  
   printf ("value of x = %d, y = %d and z = %d\n", x, y, z);   
   return 0;  
 }  

Global Variable

Variables that are declared outside of a function block and can be accessed inside the function is called global variables.
 #include <stdio.h>  
 int z; // global variable definition   
 int main ()  
 {  
   int x,y,z; // x, y local variable definition   
   x = 20;  
   y = 30;  
   z = x + y;  
   printf ("value of x = %d, y = %d and z = %d\n", x, y, z);   
   return 0;  
 }   

Formal Parameter 

Formal parameters are treated as local variables within a function and they take precedence over global variables
 int sum(int a, int b) {  
   printf ("value of a in sum() = %d\n", a);  
   printf ("value of b in sum() = %d\n", b);  
   return a + b;  
 }  

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Comments

Popular posts from this blog

How Machine Learning (ML) Work

How Machine Learning (ML) Work In this post, We will learn how machine learning work. What is machine learning (ML) ? Machine learning is a study of model that compute use to progressively improve their performance on a specific task. What is Machine learning models ? Machine learning model contains a set of instruction or mathematical formula or in simple word it contain a piece of code in which we provide some data as input and model return some output. We learn more about model and how use a machine leaning model latter. How Machine Learning (ML) Work  ? This part we will understand with an example. Suppose want to have a simple application which will take a picture and banana or apple and application will tell us either it is banana or apple or picture is neither banana nor apple. To solve this problem first we provide a set of banana picture to our model and we will tell our model that this is a banana picture similarly we will provide a set of apple...

Arithmetic Expression Problem

Arithmetic Expression Problem In this post, We will share some arithmetic operation problems. Please solve it and your answer in comment section. Solution of all problem are available here  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

HLL Infra Tech Services Ltd

HLL Infra Tech Services Ltd  Recruitment Company Name HITES Post Name Chief Engineer State India Salary Rs  32,900-58,000 Number of Vacancy 06 Eligibility Degree in Engineering Last Date 10-12-2018 Application Details Job Notification Apply Online TBD HLL Infra Tech Services Ltd Recruitment Government Jobs in  India Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Install Adminer

ADMINER APACHE Adminer is a tool for managing content in databases. In this post, We learn how to install Adminer on ubuntu. Install Adminer To install Adminer run sudo apt install adminer Configuration  Copy the configuration to your config folder and enable it sudo cp /etc/adminer/apache.conf /etc/apache2/conf-available/adminer.conf  sudo a2enconf adminer Reload Apache systemctl reload apache2 Run Adminer Open your favorite browser and type localhost/adminer/ or  localhost/adminer.php/ Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.