Variable with examples

Variable refers to a name of particular space, where values can be stored and change/manipulate in program. Every variable is assigned to specific datatype (int, float, char etc).

Variable declaration: 

Syntax: 
datatype_variable1, variable2, variable3, ..., ..., ..., ..., variableN;
Example (A):
int a, b, c;
Example (B):
float x, y, z;

Variable initialization:

Syntax:
Datatype variable_name1=value, variable_name2=value2;
Example (A):
int a=5, b=7;
Example (B):
float x=11.65, y=32.54;

Let's see examples to get better understand.

Example 1:

int a,b,c=0;

// after some line of code

a=18;
b=54;
c=a+b;

In above small line of code shows a,b and c are variable.
int is datatype assigned to specific variable.
initially a and b have no values, but we can see that c have 0.

after some line of code a and b got some values in integer (Numeric value). that values are stored in respective variable.
now next line c=a+b; 
we already assigned 0 value to c, after this c=a+b; c have new value which is addition of variable a and variable b. New value is overwrite on variable c.

Example 2:

int c=0;
float a,b,d;

// after some line of code

a=18.13;
b=54.94;
c=a+b;
d=a+b;

In this example float datatype is assigned for variable a, b and d, and integer is assigned for c.
now output is:
c=73
d=73.070000

due to difference in datatype of variable c and d, output is different. (If you are not familiar with "datatypes", kindly read about it)

Let's see this program in dos-box...

Output of program:



No comments: