First C program

                  Recently , we learned about C constant , variable and keywords . Now we have to combine them to form instructions .
Lets do this :

/* Multiplication of  floating and integer number */

#include<stdio.h>
int main ()

{
   int P ;
   float R;
 
    P = 500 ;
    Q = 8.5
    R = P * Q ;  /* Multiplication */
    printf("%f\n", R) ;
    return 0;
}  

 Let understand the above program in detail .
Form above C program  , We will understand easily that 'how it has to be written' . There are certain rule to write C programs . These are :

(a) Each instructions in a C program is written as a separate statement .
(b) The  Statements in a program must appear in the same order in which we wish them to be executed .
(c) Blank space may be inserted between two words to improve the readability of the statement .
(d) All statements should be in lower case letters .
(e) Every C statement must end with a semicolon ( ; ) , thus acts as statement terminator .


How to write comment in C program ?

        Comments are used in a C program to clarify either the purpose of the program or the purpose of some statement in the program .
  comment should be enclosed within /*  */  in C  . within /*  */ , compiler does not take as instruction .
 Also comments are written as within  //
   Example :  /* these are comments */        or    // these are comments

What is main( ) in C ? 

                          main( ) is a very important part of any C program . main( ) is one type of function . A function is nothing but a container of set of statements . In C program , there are multiple functions . In this discussion , we only talk about main( ) . All the statements that belongs to main( ) are enclosed with a pair of  second braces { } .
                        Function in C also return a value . main( ) function also return an integer value , hence there is an int before main( ) . If for any reason the statements in main( ) fail to do their intended work , then , the function returns a non zero number , which indicates , the program is wrong . 

The arithmetic operators available in C are   + , - , * ,    .

Purpose of printf( ) in C :

                 printf( ) is a ready made library function to show all the output on the screen . Once we instruct the device through the program ,it needs to displayed on the screen . with the help of this function ,we can easily handle output in C . 
                       To use this ready made function , it is necessary to use #include<stdio.h> at the starting of the program . We will discuss about this in later . 
       The general form of printf( ) function is ,
       printf("format string", list of variables);

format string can contain ,
 
 %f for print real values .
 %d for print integer values .
 %c for print character values .

'\n' use for takes new lines .

Compilation and Execution in C :

   Once you have written the program , you need to type it and instruct the machine to execute it . Two 
other tools are needed this , one is Editor & another one is compiler . Compiler convert our program in to machine language program .

 Receiving input in C :

In the first program , we assume the values of P and Q . Every time we run the program , we could get the same value for multiplication of two number . If we want to multiply for some other values , then we have require to change the whole program . 
   To make the program general , we have another function to achieve the program , which is scanf( ) .
this function is a counter part of printf( ) .printf( ) outputs the values to the screen whereas scanf( )
receives them from key board .

Lets try same example as we done at the first ,


#include<stdio.h>

int main()
{
int P;
float Q,R;
printf("Enter the values of P = ");
scanf("%d",&P);
printf("Enter the values of Q = ");
scanf("%f",&Q);
R=P*Q;
printf(" Multiplication of integer and floating number = %f\n",R);
return 0;
}

Note that use of & before the variables in the scanf( ) function is a must . & is 'Address of ' operator .








Share:

Post a Comment

For query , suggestion and others , please comment below. Don't share external link or spam on comment box . 💌

Previous Post Next Post