Monday, July 17, 2017

Simple C Program for understand C Programming

C Program rules:

      Before we start writing C program code we must remember the following rules that are applicable to all C programs:
  1. Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements.
  2. The statements in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence.
  3. Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword.
  4.  All statements are entered in small case letters.
  5.  C has no specific rules for the position at which a statement is to be written. That’s why it is often called a free-form language.
  6. Every C statement must end with a;. Thus; acts as a statement terminator.
    Let’s start writing first C program. It would be simply calculate simple interest for a set of values representing principle, number of years and rate of interest.

C Code:

/* Calculation of simple interest */
/* Author Mike Date: 18/07/2017 */
main( )
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;

/* formula for simple interest */

si = p * n * r / 100 ;

printf ( "%f" , si ) ;

Now, Let's learn how this program is works ...... 

  • Comment on the program should be enclosed within /* */.For example, the first two statements in our program are comments.
  • Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written.
  • Any number of comments can be written at any place in the program. For example, a comment can be written before the statement, after the statement or within the statement.
  • Often programmers seem to ignore writing of comments. But when a team is building big software well commented code is almost essential for other team members to understand it.
  • Technically speaking main( ) is a function. Every function has a pair of parentheses ( ) associated with it.
  • Any variable used in the program must be declared before using it. 
  • Any C statement always ends with a ;.
  • * and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /. C is very rich in operators. There are about 45 operators available in C. 
  • Once the value of si is calculated it needs to be displayed on the screen. Unlike other languages, C does not contain any instruction to display output on the screen. All output to screen is achieved using ready-made library functions. One such function is printf( ). We have used it display on the screen the value contained in si.
  • In addition to format specifiers like %f, %d and %c the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed.
  • printf( ) can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators.
    Thus, 6, 9 + 3, c and a + b * c – d all are valid expressions.


Post a Comment