C programming tutorial

                 PREVIOUS                                                                                   NEXT CHAPTER

variables in C

 

Variables are used to store values.You will require variables If your program will need a value from user or it need to calculate something .

When we write

int a=2;

It means that a is the name given to storage location in which an integer value will be stored.

If we do a=a+2.This means increment the value stored in named location 'a' by 2 then assign it to a i.e assign it to itself.Hence now a will contain 4 inside it.= is known as assignment operator in C.It assigns the calculated value of its right side to left variable.In above example a+2 was in its right side.First its value was calculated then calculated value was assigned to right side i.e a;

 

How to declare variables in C 

Variable is declared in following way

(type of variable or data type ) (name of variable)

int a;

int b,c,d;

char d,e;

float f;

type of variable means the type of value the variable will hold, If it will hold integer, type will be int.If it holds character, type will be char.If it will hold value like 2.34 its type will be float.Type of varriable is also known as data type.

 

In C variables should be declared at the starting of main function.We cant declare variables in the middle of program.

variable initialization in C

Initialization of variable means assigning value to it.Following is the way we assign value to variable.

a=4;

Now a will have value 4

b=5,c=6;

Now b will have value 5 and c will have value 6.

We can initialize variable at the time of declaration also in one step;

int a=2;

Types of variable

Following are the fundamental types of variable

int integer

char character

float real values

 

There are more we will see during the course.

 

 


               PREVIOUS                                                                                   NEXT CHAPTER