Monday 25 October 2010

Variables

Defn:

A variable is an identifier which occupies some part of the memory.

Why should we use Variable?
Which is used to store data. Which store only one data.

syntax for declaration:
datatype variable;

Example:
int a;

Syntax for initialization:
datatype variable=data;

Example:
int a=10;//Here, a is a variable of integer type.

Rules for writing variable name in C
1.    Variable name can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only.
2.    The first letter of a variable should be either a letter or an underscore. But, it is discouraged to start variable name with an underscore though it is legal. It is because, variable name that starts with underscore can conflict with system names and compiler may complain.
3.    There is no rule for the length of length of a variable. However, the first 31 characters of  a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.
In C programming, you have to declare variable before using it in the program.


Types of the variables:

1. normal variable or variable
2. pointer variable or Pointers

What is Garbage Value?
In a language like C, when you declare a variable, like:
int a;

a small block of memory is allocated to the variable a. However, we have only declared the variable, and not initialized it, which means that the block of memory that has been allocated to the variable still contains some value that has been left over from previous programs and operations. That value is called a garbage value. This may lead to erroneous results in programs.

To avoid this, declare and initialize variables like this:
int a = 0;

Nowadays, many languages initialize variables automatically. In that case, you won't have an issue with garbage values,


You  may like the following posts:

Download Data types ppt
Download Data type ppt
Download Variables and Constants ppt
C Tokens
identifiers

No comments:

Post a Comment