Variables in C Programming

Variables are simply the names given to the memory location for any data whose value may be changed during the program execution i.e variable is the name of memory location or memory address of any data. The value stored in the variable can be changed.

Each variable in C has a specific type which determines the storage size of the variable's memory along with the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore but not with a digit. Since C Programming is case sensitive, upper and lowercase letters are distinct.

Sr.No. Type Description
1 char Typically a single octet(one byte). It is an integer type.
2 int The most natural size of integer for the machine.
3 float A single-precision floating point value.
4 double A double-precision floating point value.
5 void Variable that returns no value

Rules for naming a variable in C Programming

* The variable name must be start with an alphabet or an underscore
Example : 
int 1a; //invalid
Int _a; //valid

* The variable may be a combination of an alphabet and digit along with an underscore but the first character should not be digit.

* No space or special character is allowed while naming a variable.

Example : 
int Roll_1;//valid
Int Roll 1;//invalid
Int Roll#1;//invalid

* same name with diffent sentence cases are recognized as two separate entities
Example :
int cat = 1;
int CAT = 2;

Here, cat and CAT can store two different values as the sentencase in those two names are different.

* The variable name should not be any keyword.
Example : 
int do;//invalid (because do is keyword)
 
Here, you can see the basic variable types and the type of data they can store.

Defining Variables in C Programming

Variables are required to be defined in a C Program before you can actually execute any operations on them. A variable definition contains the data type a variable can hold where one or multiple variables of the same type can be defined.

Example :
int a;
char i, j, k;

While declaring the variables, the intial values can be assigned to those variables as well.

Example :
int a = 5, b = 4;

Unless the values are assigned to the variables at the time of declaration, the initial values of such variables are assumed as undefined. 

A variable can be defined only once in a file, function or a block of code.

Example :

#include <stdio.h>

int main () {

  int a = 5, b = 10;
  int c, d;

  c = a + b;
  printf("Sum of a and b is : %d \n", c);

  d = 45/c;
  printf("value of d : %d \n", d);
}

Output : 

Sum of a and b is : 15

value of d : 3

Scope of Variables

A scope in any programming is the area within which a defined variable can be accessed. Beyond that area, the same variable becomes an undefined entity. On the basis of the scope, variables can be classified into three categories in C programming.

Global Variable

A global variable is a variable that is proclaimed outside any capacity, usually at the global declaration section of a C program and is available to all capacities throughout the program. Global variables hold their values throughout the lifetime of the program and can be accessed inside any of the functions defined for the program.

Local Variable

A local variable is a variable that is declared inside a function or block of a C program. They can be used only inside that function or block of code. Local variables, if tried to use outside the function where they are defined will be an undefined entity. If a variable is defined as a global variable as well as a local variable, the function inside which the variable is used will priotize the value given inside the same function over the global one.

Example :

#include <stdio.h>

/* global variable */
int sum;

int main () {

  /* local variable */
  int a, b;

  /* initialization */
  a = 10;
  b = 20;
  sum = a + b;

  printf ("The sum of %d and %d is %d\n", a, b, sum);

  return 0;
}	

0 Like 0 Dislike 0 Comment Share

Leave a comment