Sunday 5 February 2012

C program cont...(session 2)


               To go to prev. section. I will start with a small program then explain the elementary items of it so that you should be able to identify elementary items of any program so easily and understand the logic or the flow clearly.
                Lets take an example Fahrenheit temperatures to centigrade or Celsius equivalents the formula to do this is oC=(5/9)(oF-32) 

The following is the program to do it

#include <stdio.h>

/* print Fahrenheit-Celsius table

for fahr = 0, 20, ..., 300; floating-point version */


main()

{

float fahr, celsius;

float lower, upper, step;

lower = 0; /* lower limit of temperatuire scale */

upper = 300; /* upper limit */

step = 20; /* step size */

fahr = lower;

while (fahr <= upper)

{

celsius = (5.0/9.0) * (fahr-32.0);

printf("%3.0f %6.1f\n", fahr, celsius);

fahr = fahr + step;

}

}


In the above program the lower,fahr,Celsius,upper,step are variables of type float. What does it mean? This can be explained in two perspective that is from programmers perspective and from compiler perspective


From programmers perspective:


1.         It’s a rule that all variables used in program should be declared.

From compilers perspective:


            The above should make you think why is that rule, what is the need, its because, the variable declaration tells the compiler that these variables are used in this program and these variables are of which data type. In turn the compiler allocates the memory space base on the data type that is for char 8 bit and for int 16(natural size of integers on the host machine) bit memory in 16bit machine. When ever the user/programmer use the variable name it points the value in this memory.

Basic data types:


 hope that gave you enough info about the importance of knowing the data types and how internally memory allocation is done.

Variable:


         A variable is a data name used to store values of some type, these name refer to memory.(Note: its always good habit to have a variable in a meaning full way).

Rules to be followed:


         1 Must begin with a letter.(can start with _ but its not good way to declare a variable like that)
         2 it should not be key word
         3 white space and other special symbols except _ not allowed.

Declaration of variables:
Syntax:
         datatype var1,var2,....varN;
         (e.g) int total;


Operator and Expressions:
1. Arithmetic operators(+,-,*,/,%)(add,diff,product,division,mod)
2. Relational operators(<,<=,>,>=,==,!=)(less than,less than or equal to,greater than,greater than or
                                    equal to,equal to,not equal to)
3. Logical operators(&&,||,!)(AND,OR,NOT)
4. Assignment operator(=)
5. Increment / Decrement(++,--)
           This has two types post and pre
            Lets explain with a help of example i++ here post increment that is ++i means pre increment.
6. conditional operator(exp1?exp2:exp3)
            let me explain with an example x=(a>b)?a:b; that is here exp1 is (a>b) if that is true a will be assigned to x or b will be assigned to x
7. Bitwise operator (&,|,^,<<,>>)(bitwise AND,bitwise OR,bitwise EXCLUSIVE OR,shift left,shift right)
           These bitwise operators are used for manipulation of data at bit level
8. Special operators(pointer operators & and *) and member selection operator(. and ->)

click here to see first session

No comments:

Post a Comment