C Variables

<<Previous

Next >>





The Variable is a data name that is used to store a date value. The variable is nothing but a box in your computer's memory that hold the different type of data such as integers, floating points, characters and strings. It is also called an identifier. Variable can have the unique name that differentiate it from the other variable. The variable is not only having the names, they also have types. These types can convey the information to the compiler about the amount of memory that will have to allocate to the variable while program execution. During the program execution, the value of the variable can be changed.

Many kinds of variables may exist. That means, integer variable can hold only the integer data, floating point variable can hold only the floating point data and so on. Following are the list of different variables:

  • int---holds the integer data
  • char--- holds the character data
  • float---- holds the floating point data
  • double--- holds extremely large or extremely small floating point data

In addition to that C programming can also include other variable types such as void, enumeration, volatile, array, string, pointer, union and so on. These will be discussed in later section.

Rules for defining variables

  • The variables must begin with a character without space
  • It should not start with a digits.
  • Underscore is permitted between the variable.
  • The variable length can varies from 1 to 31 characters.
  • The variable should not be a keyword.
  • The variable name may be a combination of lower case and upper case letters. The C is case sensitive so we need to take much care while defining the variable name. Ex: ‘ Sum ’ and ‘ sum ’ are not same variables.

Following are the valid variable names:

myname   Use_97    Weight    height    AVRg

Following variables are invalid:

int   const    90usd

Defining the variable

The variable must be defined before they are used in the program. The variable definition tells the compiler the data type of the variable being declared and helps in allocating the memory. To declare the variable in C program, we need to specify the data type and followed by list of variable names.

Syntax:

Data-typename  Variablename

Here Data-typename can be an any one of the valid data type such as int, char, float, double etc. The Variablename is an identifier name.

For example;

 int age;
 char m;
 float average;
 double result;
 

We can define more than one variables of the same data type on the same line that are separated by comma. For example, you have to define three variables of double data type on the same line.

double result, average, a ;

Initializing the Variables

We assign a initial value to the declared variable. For initialization, we use the assignment operator ‘ = ’ which puts the initial value to the variable.

Syntax:

variable-name = data;

Here the variable-name is the name of the variable where the value is stored. The data can be a number, character or result of the other expression.

For example:

  int a=10;
  char ch='C';
  int a,b,c= 20;
  

Where to declare the variable?

There are two places in which the variables are declared.

  • One place is outside of all function. This kind of declaration are called global variables declaration. These variable can be used by all functions.
    	 #include<stdio.h>
    
    	 int avg;
    	 				/* Global variables */
    	 float result;
    
    	 int main()
    	 {
     		/* rest of the program statements */
     	 }
     
  • Other place is inside the open and close curly brace,that means '{}'. These variables are called local variables which work only inside their {} brace.
   #include<stdio.h>

   int main()
   {

       int mark1, mark2;   /* Local variables */

	float avarage;

 	/* rest of the program statements*/
    }
   

<< Previous

Next >>