Saturday 18 January 2014

making first program 'Sum'

Now we write our first program with C. Our program is use to compute the sum of 10 and 20. It is a basic program to understand the little things that including the programming language. We taking three variable as x, y and z.

x=10
y=20
z=x+y



The sum should be 30 but how it works in program and how to write ? Before this we have to learn what is it ?

Rule


Here x,y and z are variable and its value 10 and 20 is constant. = and + are arithmetic operators for performing a particular operation.


Before using any variable in program we must declare first. To declare a variable means to specify type of information and the most important thing that all variable which is declared in a function can alive in its own function. We give an example to understand this all rule.

The following are the basic and most important keyword for declare variable/data type.


Data/Info Name of Data type Keyword Format specifies
1,554,78 etc Integer nm int %d
2.54 ,5.5887 etc Fractional/real nm float, double %f
O,A,c,l,e etc Character char %c


Note: All the statements within a function should be terminated by semicolon ';'. Because of semicolon rule C language is also called as free form language.



Our first Program



int main()
{
int x,y,z;
x=10;
y=20;

z=x+y;
return 0;
}


The above program is our first program. The first line int main() is a function named as main(). Without main() function program not work. int is a return type used because this is an integer based program and it will return an integer value. We can also use void instead of int  when we write void we don't have to write return 0 because void is not return value.

After function we must write all of his code in block of codes, block of codes means between opening curly braces and ending curly braces

Next we declare three variable x,y and z and the data type of every variable is int ,int can store 32 bit of memory. Because of declaration memory will allocate in ram for these three variable. It will send request for allocation of memory send to Operating System.
consider that above rectangle is ram and here allocation for x, y and z is done, while variable x, y and z are int type so it can hold only 4 bytes/32 bit. Means that in small rectangle of  x , y and z only 4 byte or less value can be place.

In next line x=10 and y=20 the assignment for variable x and y is done. means the box/rectangle of x is now holding the binary number of 10 and y is holding binary number of 20. In statement z=x+y four steps are take place first the value of x is picked up second the value of y is picked up third the addition of the value of x and y will proceed and the last step is the addition of x and y is assign to z in binary form
 

Note : The above program has no output. Because we had not use printf() function. In my next post I will describe predefine function and header files.

No comments:

Post a Comment