facebook like button

23 March, 2011

program 2: addtition of two numbers

The code:
----------------------------------------------------------
#include<stdio.h>
main()
{
    int i,j,k;
    i=5;
    j=4;
    k=i+j;
    printf("Sum = %d\n",k);
}
----------------------------------------------------------
    Here i,j,k are variables(storage) to store the integer values. The keyword int before them indicates that those will be holding the integer values. This step is called declaration of variables. Here in declaration statement memory is allocated (accommodation is arranged) to the variables i,j,k. Statements like k=i+j; are called assignment statement(i.e. i=5,j=4 are also assignment statements). In these cases the value is assigned to the left side variable. So we see that the value can be a 'literal' or an 'expression'. If it is an expression then first its value is calculated and then this calculated value is assigned to the left hand side variable.
     Here we have seen 2 new terms  'literal' and 'expression'. We'll be going into the details of these terms later. For now only assume that all numbers, alphabets and symbols which have not been declared as variables are 'literals' and any arithmetic or logical function of variables that can give some value is called an 'expression'.
     Look at the printf statement and  then see the output of the program which is given below:
Sum = 9
  We see that %d is replaced by 9 that is the sum(value of k). Yes you got it right, in printing wherever we want to print the value of any variable, just put a '%d' inside the printf statement and after that write the variable name separated with a comma.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply