Variables in C language – Free C programming Tutorials

In this segment you will study about the variable in C and along with this we will discuss how to declare the variables and how to initialize them in c language.

  • Variable can be defined as a named location in a memory where a program can manipulate the data and also these locations are used to hold the value of the variable.
  • The value content of variable may get change during the execution of the program.
  • C variable must belong to the data type such as int, float, char, void etc.

RULES FOR DECLARING A VARIABLE IN C:

  • Variable names should start with letters (A-Z,a-z) or underscore(‘_’) only.
  • Variables names are case sensitive i.e. upper case and lower case letters are different.
  • Variable names can be constructed with digits (0-9), letters (A-Z,a-z).
  • No special symbols are allowed in variable names other than underscore (‘_’).
  • Reserved words cannot use as a variable name in C.

For example:
sum, height123 , _value


DECLARING & INITIALIZING a VARIABLE in C:

  • Variables must be declared in the C program before its use in the program.
  • Memory space is not allocated for the variable while declaration and the memory space is allocated for the variable only on its definition.
  • Variable initialization means assigning a value to the variable.
Type  Syntax
Variable declaration data_type variable_name;
Example:int x;char ch;
Variable initialization data_type variable_name = value;
Example:
int x = 50;
char ch=’A’;

THERE ARE TWO TYPES OF VARIABLES IN C PROGRAMMING LANGUAGE, THEY ARE :

Local variable and

Global variable

Local variables:

Local variables are defined as the variables whose scope will be within the function only that is these variables are declared within the function and can’t be accessed outside the function.
In the code given below, m and n variables are having scope within the main function only, therefore these variables are not visible to any other functions such as test function.
Likewise, a and b variables are have scope within the test function only.

EXAMPLE OF PROGRAM FOR LOCAL VARIABLE:

#include<stdio.h> // header file

// Declaration of function test()
void test();

int main()
{
    int m = 10, n = 20;
// m, n are local variables of main function

/*scope of m and n variables is within this main function only.
These variables are not visible to test function.*/

    printf("\n values : m = %d and n = %d", m, n); // function to display the output 

    test();  // calling of function test
}

// Definition of function test()
void test()
{
int a = 30, b = 40;
// a, b are local variables of test function
// These are not visible to main function.
printf("\nvalues : a = %d and b = %d", a, b);
}<br>

Output:

values : m = 10 and n = 20
values : a = 30 and b = 40

Global variables

Global variables are defined as the variable whose scope will be throughout the program that is these variables can be accessed from anywhere in the program.
And global variable is defined before writing i.e. outside the main function so that, these variables are noticeable to main function and all other functions.

EXAMPLE OF PROGRAM FOR GLOBAL VARIABLE :

#include<stdio.h>  // header file
void test();  // Declaration of function test()

// Declaration and initialisation of global variables
int m = 14, n = 24;
int a = 10, b = 20;

int main()
{
   printf(“When variables are accessed from main function");   // to display output
   printf("\n values of : m=%d , n=%d , a=%d , b=%d", m,n,a,b);

// Calling of function test()
   test();
   return 0;
}

// Definition of function test()
void test()
{
   printf("\n When all variables are accessed from test function");
   printf("\n values: m=%d , n=%d , a=%d , b=%d", m,n,a,b);
}

OUTPUT:

All variables are accessed from main function
values of : m = 14 , n = 24 , a = 10 , b = 20
All variables are accessed from test function
values : m = 14 , n = 24 , a = 10 , b = 20

This is all about variables in C language, if you have any query please comment below.

Leave a Comment

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