Saturday, October 26, 2013

Variable initialization


By default every variable contains an unpredictable value called garbage value. Assigning initial value to the variable is known as variable initialization. A variable can also be initialized during its definition.
E.g.    1) int a,b;
              a=b=100;
Assign value 100 to both the variables a and b.
          2) float area;
              int z;      
              area=3.14159*4*4;
Assign 50.2654 to variable area.       
             z=area+100;
Assign 150 to z. Here z is an integer variable so it truncates the decimal part of the result.
          3) int i=0,j=1;
Assign 0 to variable I and 1 to variable j.

Another way to declare variables
You can adopt this totally new way to declare variables you use in your programs. How? The following program demonstrates this.

#include <stdio.h>
#include <conio.h>

main(i,j,k)
{
   clrscr();
   i=2,j=5;
   k=i+j;
   printf(“The sum of %d+%d=%d”,i,j,k);    
   getch();
}

Output:
#-------------------------------------------------------------------------------#
The sum of 2+5=7
#-------------------------------------------------------------------------------#
Note that if you do not specify the data type of the variable declaring inside the parenthesis of main(), then the compiler by default treat them  as an integer variables. But, if you want to specify other type of variables, say float or char, then it is necessary to explicitly specify the data type.
Program 5:

/* Using float  variables. */
#include <stdio.h>
#include <conio.h>

main(float i,float j,float k)
{
   clrscr();
   i=2.5,j=5.9;
   k=i+j;
   printf(“The sum of %f+%f=%f”,i,j,k);      
   getch();
}

Output:
#-------------------------------------------------------------------------------#
The sum of 2.500000+5.900000=8.300000
#-------------------------------------------------------------------------------#
/* Using char variables. */
#include <stdio.h>
#include <conio.h>

main(char ch1,char ch2,unsigned char ch3)
{
   clrscr();
   ch1=’A’;
   ch2=’B’;
   ch3=ch1+ch2;
   printf(“The sum of %d+%d=%d”,ch1,ch2,ch3);
   getch();
}

Output:
#-------------------------------------------------------------------------------#
The sum of 65+66=131
#-------------------------------------------------------------------------------#
Remember that the ASCII value of A is 65 and the ASCII value of B is 66. So, in the above program we are performing operation on the ASCII values of the variables rather than two characters.

Arithmetic opearators
Arithmetic operators are used to build simple as well as complex expressions. C uses following arithmetic operators.
+       :Addition
-        :Subtraction
*        :Multiplication
/        :Division
%      :Modulus
We will explore these operators time by time later on.

 The assignment statement:
The main statement in C for carrying out computation and assigning values to the variables is the assignment statement. the general form of assignment statement is:
                             result=expression;
the expression is evaluated and then the value of the expression is assigned to the result. It is important to note that, the value assigned to the result must be the same as that of result. where result is any valid variable name. expression must be any valid C expression. The expression can be single variable, a single constant or involves variables and constants combined by the arithmetic operators.
For example, the following assignment statement:
          average=(a+b)/2;
assigns half the sum of a and b to the variable average. have a look at the another example:
          i=3;
Assign 3 to the variable i.
Perimeter=2.0*(length*breadth);
Assign the value of rightmost expression to the variable on the left hand side i.e. perimeter.
          i=j=k=l=500;

assign value 500 to all variables i.e. i, j, k, and l.

No comments:

Post a Comment