Monday, October 28, 2013

Playing with variables, constants and keywords..


To write better programs, it is necessary to understand the basic building blocks of this beautiful language. This chapters mainly deals with variables, constants and keywords. So, get prepared for this wonderful journey.

What is Variable?
A variable is an entity whose value can be changed during program execution. A variable can hold only one value at a time i.e. when we assign new value to the variable, previous value gets lost.

The syntax for declaring variable is:
data type var1,var2,….varn;

Where,
data type may be any data type from the following:

int, float or char.
var1,var2,…varn are number of variable of identical data type.

E.g.      int age;                                                      
           float salary,da,hra;
           char code,gender;

Here age is an integer variable which can hold integers only (whole numbers (positive or negative, but it does not contain any decimal point). Examples are: 

100,   0,       -707 etc.

salary, da and hra are float variables which holds floating point values (which must contain a decimal point). Examples are:
0.5,    -3.5,  .1       ,1 etc.

code and gender are character variables which can hold only one character at a time. The character must be entered in single quotation marks(‘ ’).
Examples are:
‘&’,     ‘+’,     ‘9’,     ‘R’ etc.

Consider the following program.
Program 1: Write a program (WAP) to receive the temperature in Fahrenheit degrees and convert it into degree centigrade.

#include <stdio.h>
#include <conio.h>
void main()
{
          float far_temp,deg_ce_temp;
          clrscr();
          printf(“Enter temperature in Fahrenheit degrees:”);
          scanf(“%f”,&far_temp);
          deg_ce_temp=5*(far_temp-32)/9;
          printf(“Temperature in degree centigrade is=%f”,deg_ce_temp);
          getch();
}

#----------------------------------------------------------------#
Enter temperature in Fahrenheit degrees:100
Temperature in degree centigrade is=37.777779
#----------------------------------------------------------------#

In the above program far_temp and deg_ce_temp are variables. A variable must be defined before using it in the program.

Note: The section where you declared your variables is known as type declaration section.

In our program, the line
float far_temp,deg_ce_temp;

is a type declaration section. Whatever numbers of variables you are going to use in your program must be declared in this section. this section may covers one or more lines depending on the nature of the program. The following chunk of code typial type declaration section.

E.g. char item[20];
       int partno;
       float cost;

Warning: After main() there must be type declaration section if you are using variables in your program. And then write other statements or functions. Otherwise the compiler flashes the following error message:
          “Declaration section is not allowed here.”

Save the above program as Error1.c and interchange the following two lines of the program.
float far_temp,deg_ce_temp;
        clrscr();
with
        clrscr();
float far_temp,deg_ce_temp;


Now, save the program. And try to compile it. Your compiling window will show 1 error in it. Press Enter and you get the above error message.

No comments:

Post a Comment