In this article Types of Variable in C we give the information about Scope of a variable are used to show the lifetime of that variable. It can be used or accessed only in the block or function in which they are declared.

Types of variable in c:

Local and Global variables:

As we know, Variable is used to store the data or information it is the name of the place in the memory. In C language we must declare the variable first then use it. If we have to use any variable in our program, then we have to declare it first.

Types of variable in c:

There are two types of variable such as

  1. Local Variable
  2. Global Variable.

On the basis of where is the declaration of the variable, we can tell that which variable is local variable and which variable is global variable.

Scope of variable:- 

Scope of a variable are used to show the lifetime of that variable. It can be used or accessed only in the block or function in which they are declared.

Example:

#include<stdio.h>

void fdata()

{

int  y = 15;

}

In the example, we have declared a variable named y inside the fdata() function, whose scope will remain only till the fdata() function, we cannot use it in any other function and if we try to do so, the compiler gives an error.

The scope of a variable is totally depends on it is declaration.

There are three way to declare the variable in C Programming.

Inside a function or block

Outside the function or block

Inside the function parameter at the time of function definition.

Local Variables in C

When we declare a variable inside a function or block, it is called a Local Variable.

Local variable use only inside the block or function in which that variable is declared.

The lifetime of a local variable is only equal to the lifetime of the function or block in which it is declared.

#include <stdio.h>

void main ()

 {

  Int p, q;    //local variable declaration

  int sum;

  p = 15;     // actual initialization

  q = 25;

  sum = p+q;

   printf (“%d + %d : %d\n”, p,q,sum);

 }

Output

sum of 15 and 25 is : 40

In this example, we have created three variables named p, q and sum inside the main() function, whose scope is limited to this main() function and we cannot use it in any other function.

Advantages of Local Variable in C

  • When we declare a variable inside a function, its scope is limited to that variable only, so if we want, we can create a variable with the same name in two different functions, this will not cause any problem in the program because the scope of both is will be limited to that function only.
  • The memory that a local variable gets is available only till the function in which the local variable is declared is run in the computer. As soon as the function ends, the variable is released from its memory and that memory can be used by any other variable of the program. Can use
  • The probability of local variable is higher than that of global variable.

Disadvantages of Local Variables in C

  • The scope of a variable is limited so that a variable declared in a function cannot be used by any other function.
  • The debugging process becomes a bit difficult.
  • Data sharing is not possible in local variables.

Types of variable in c:

Global Variables in C

Global variables declare in-between the header files and main function.

A main advantage of global variables is use these variables in any block or function of the program. The value of this variable can be used and changed by any function.

Since global variable is declared outside the main function therefore its scope is global. So that we can use these variables in any block or function.

The lifetime of a global variable is equal to the lifetime of the entire program and as soon as the program ends, the global variable also gets destroyed and the space occupied by it in memory is also released.

You always declare global variable in-between the header files and main function.

#include <stdio.h>

 int sum;          // global variable declaration

void main ()

{

  int p, q;    // local variable declaration

  /* actual initialization */

  p = 10;

  q = 20;

  sum = p + q;

  printf(“value of p = %d, q = %d and sum = %d\n”, p,q,sum);

 }

Output -:

value of p = 10, q = 20 and  sum = 30

In this example, we have declared a variable named sum at the very beginning of the program. The variable sum is a global variable since it is not declared inside any function or block.

Note:  if the name of local and global variable is same then in this case we give priority to the local variable.

Example-:

#include <stdio.h>

int sum = 20;  // global variable declaration

void main ()

{

  int sum = 10;     // local variable declaration

  printf (“Value of Sum = %d\n”,  sum);

}

Look at this example, in this a variable declared at the top of the program and another variable declared inside the main() function both have the same name.

In this case, when we print the value of the sum variable in the main () function, then the value of the local variable sum created inside the main () function is printed because when it comes to priority between local and global variables, then the local variable Highest priority is given.

Output -:

Value of sum= 10

Advantages of Global Variables:

  • Global variable is used or accessed whole program that is any function in the program.
  • It is declared only once in the program.
  • It is very useful when all the functions of the program are accessing the same data.

Disadvantages of Global Variable:

  • You can change the value of global variable at any time by any function.
  • If we use too many global variables in our program, then the possibility of error in the program increases.
  • The life of the global variable depends on the life of the program, as long as the program remains in the memory, the global variable will also remain, in such a situation, even when we do not have to use the variable variable, such memory is found, which is a loss of memory.

Formal parameters

While defining a function, when we declare a variable inside the function parentheses, it is called Formal Parameters and the scope of such variable is local.

// Local and Global variable Program:

// types of variable – Local variable & Global variable

#include<stdio.h>

#include<conio.h>

int n;                   // global variable

void square();           // function decration

void sum();            // function decration

void main()

{

int n=50;    // local variable             /*  if the name of local and global variable is same then in this case we give priority to the local variable.         */

clrscr();

printf(“\n\n\n Value of N: %d”,n);

square();  // function call

sum();

getch();

}

void square()              // function defination

{

n=7;             // Global variable

printf(“\n\n Square of N: %d”,n*n);

}

void sum()

{

int m=5;          // local variable

n=6;                // Global variable

printf(“\n\n Sum of Two numbers: %d”,n+m);

}

Output:

Value of N: 50

Square of N: 49

Sum of Two numbers: 11

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *