WHAT ARE VARIABLES?

A variable  is a named location in memory that is used to hold a value. Variables can hold one value at a time. The value of variable can be changed during execution of program . It is different from constant .They can be viewed as the names given to the memory location so that we can refer to it without having to memorize the memory address. The size of the variable depends upon the data type it stores.

C VARIABLE SYNTAX

                          data_type variable_name ; // defining single variable

                        data_type variable_name1, variable_name2;  // defining multiple variable

            Here,

                        data_type: Type of data that a variable can store.

                        variable_name: Name of the variable given by the user.

In coming session we will discuss about data types. If you want to learn know about data type click here.


RULES FOR NAMING VARIABLES IN C

  • A variable name must only contain alphabets, digits, and underscore.
  • A variable name must start with an alphabet or an underscore only. It cannot start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword.

C VARIABLE DECLARATION

Variable declaration in C tells the compiler about the existence of the variable with the given name and data type. When the variable is declared compiler automatically allocates the memory for it.

Example

                int age;

                char name;


C VARIABLE INITIALIZATION

Initialization of a variable is the process where the user assigns some meaningful value to the variable.

Example

                int var; // variable definition

                var = 10; // initialization

                                 or

                int var = 10; // variable declaration and initialization

DATA TYPE, SIZE, VALUE RANGE AND EXAMPLE 

Data type

Size

Value range

Example

char

1 byte

-128 to 127 or 0 to 255

char letter = 'a';

int

2 or 4 bytes

-32,768 to 32,767 or

-2,147,483,648 to 2,147,483,647

int number = -100;

unsigned int

2 or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

unsigned_int positiveNumber = 100;

long

4 bytes

-2,147,483,648 to 2,147,483,647

long number = 1e9 + 7;

float

4 bytes

1.2E-38 to 3.4E+38

float decimal = 2.14;

double

8 bytes

2.3E-308 to 1.7E+308

long number = 1e18;