Introduction
In the C programming language data types refers
to an extensive system for declaring variables of different types. The language
itself provides basic arithmetic types and syntax to build array and compound
types. A variable is just a named area of storage that can hold a single value .
The C language demands that you declare the name of each variable that you are
going to use and its type, or class before you actually try to do anything with
it.
A computer memory is made up of registers and
cells which are capable of holding information in the form of binary digits 0
and 1 bits. This is the way information is stored in a memory just like we
memorize numbers using 0 to 9. Collection of 8 bits is called a byte. Normally
the CPU does not accessed memory by individual bits. Instead, it accesses data
in a collection of bits, typically 8 bits, 16 bit, 32 bit or 64 bit. The amount
if bits on which it can operate simultaneously is known as the word length of
the computer. When we say that Pentium 4 is a 32 bit machine, it means that it
simultaneously operates on 32 bit of data. Data is stored in the memory at
physical memory locations. These locations are known as the memory address. It
is very difficult for humans to remember the memory addresses in numbers like
46735, so instead we name a memory address by a variable.
Variables are a way of
reserving memory to hold some data and assign names to them so that we don't
have to remember the numbers like 46735 and instead we can use the memory
location by simply referring to the variable. Every variable is mapped to a
unique memory address.
Type of Variable : The Programming
language C has two main variable types.
- Local Variables : Local
variables scope is confined within the block or function where it is
defined. Local variables must always be defined at the top of a block. When
a local variable is defined it is not initialized by the system, you
must initialize it yourself. When execution of the block starts the variable
is available, and when the block ends the variable 'dies'.
- Global Variables : Global variable
is defined at the top of the program file and it can be visible and modified
by any function that may reference it.
Declaration of variables : In order to
use a variable in C++, we must first declare it specifying which data type we
want it to be. The syntax to declare a new variable is to write the specified of
the desired data type (like int, bool, float...) followed by a valid variable
identifier.
int sum;
float average = 2.34;
char name = 'c';
Example : The simple example of a local
variable used.
Code
main()
{
int i=4;
int j=10;
i++;
if (j > 0)
{
printf("i is %d\n",i);
}
if (j > 0)
{
int i=100;
printf("i is %d\n",i);
}
printf("i is %d\n",i); }
}