VARIABLES


VARIABLES

A variable is a named memory, that can be used to read and write information. In this sense, variables are nothing more than "buckets" that can be used to store whatever values are needed for a specific computation. we have different types of variables for the various contents that can be stored in it.      
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer. Variable is a data name which can be used to store a data value. A variable is referenced to by an identifier, and denotes a memory area that can be manipulated through the use of the identifier.
The difference between a variable and an identifier is the same as between a person and his or her name. A variable is not an identifier. A variable has an identifier. It also has a type, and (if it is initialized) a value. Unlike constants that remains unchanged during the execution of a program, a variable may take different values at different times during execution.

Naming Variables


The name of variable can be called identifier or variable name in a friendly way. Of course, the variable name should be meaningful to the programming context. It has to follow these rules:
  1. The name can contain letters, digits and the underscore.
  2. The First letter must be a letter or the underscore.
  3. Maximum distinguishable length should be 8 characters.
  4. Keywords cannot be used as variable name.
  5. White spaces are not allowed.

Some valid examples are total_avg, John, int_type, john  
Some invalid examples are 1avg, John$, int ,total avg , 134, %

Declaring Variables

To declare a variable you specify its name and kind of data type it can store. The variable declaration always ends with a semicolon, for example:
Syntax:            data-type var1,var2,var3……varn;
           int count;
           char c;
           float avg;

You can declare variables at any point of your program before using it. The best practice suggests that you should declare your variables closest to their first point of use so the source code is easier to maintain. In C programming language, declaring a variable is also defining a variable.

Initializing Variables

You can also initialize a variable when you declare it, for example:

int count=0;
char c=’D’;

No comments:

Post a Comment