Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. No Comments on Types of Variables in C Language

In this article we will learn about types of variables in C language. We will start with what is variable and then learn about types of variables like local variables and global variables.

 

A variable is a memory location inside memory which is referred using a name. The value inside a variable changes throughout the execution of the program.

 

Based on where the variable is declared in the program, variables can be divided into two types. They are:

  1. Local Variables
  2. Global Variables

 

Local Variables

 

A variable is said to be a local variable if it is declared inside a function or inside a block. The scope of the local variable is within the function or block in which it was declared.

 

A local variable remains in memory until the execution of the function or block in which it was declared in completes. Let’s see the following example:

 

local-variable-ex

 

In the above example, the variable x is a local variable with respect to the main function. Variable x is not accessible outside main function and x remains in the memory until the execution of the main function completes.

 

Global Variables

 

A variable is said to be a global variable if it is declared outside all the functions in the program. A global variable can be accessed throughout the program by any function.

 

A global variable remains in the memory until the program terminates. In a multi-file program, a global can be accessed in other files wherever the variable is declared with the storage class extern.

 

Types of variables and their scope and lifetime can be summarized as shown below:

 

Type of Variable Declaration

Location

Scope

(Visibility)

Lifetime

(Alive)

Local Variable Inside a function/block Within the function/block Until the function/block completes
Global Variable Outside a function/block Within the file and other files marked with extern Until the program terminates

 

C program to demonstrate local and global variables:

#include<stdio.h>
#include<conio.h>
int g = 10;
main()
{
    int x = 20;
    clrscr();
    printf("Inside main, g = %d",g);
    printf("\nInside main, x = %d",x);
    {
        int y = 30;
        printf("\nInside block, g = %d",g);
        printf("\nInside block, y = %d",y);
        printf("\nInside block, x = %d",x);
    }
    printf("\nOutside block, g = %d",g);
    /*printf("Outside block, y = %d",y);*/
    printf("\nOutside block, x = %d",x);
    getch();
}

 

In the above example, g is a global variable and x is a local variable with respect to main and y is a local variable within the block. The variable y cannot be accessed outside the block. That is why the printf statement outside the block accessing the value of the variable y has been commented out.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

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