The First C Program

Once armed with the knowledge of variables, constants & keywords, the next logical step would be to combine them to form instructions. However, instead of this, we would write our first C program now. Once we have done that we would see in detail the instructions that it made use of. The first program is very simple. It calculates simple interest for a set of values representing principal, number of years and rate of interest.

/* Calculation of simple interest */
/* Author: gekay Date: 25/06/2016 */
 
 #include <stdio.h>

int 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\n", si);
    
    return 0;
}

Let us now understand this program in detail.

Form of a C Program

Form of a C program indicates how it has to be written/typed. There are certain rules about the form of a C program that are applicable to all C programs. These are as under:

Comments in a 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. 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.

Here are a few things that you must remember while writing comments in a C program:

/* formula for simple interest */
si = p * n * r / 100 ;

statement, after the statement or within the statement as shown below.

/* formula */si = p * n * r / 100;
 si = p * n * r / 100; /* formula */
 si = p * n * r / /* formula */ 100 ;
/* Cal of SI /* Author: gekay date: 25/06/2016 */ */

is invalid.

/* This comment has three lines in it */

Such a comment is often called a multi-line comment.

// Calculation of simple interest // Formula

What is main() ?

main( ) forms a crucial part of any C program. Let us understand its purpose as well as its intricacies.

int main( ) { statement 1 ; statement 2 ; statement 3 ; }

Variables and their Usage

We have learnt constants and variables in isolation. Let us understand their significance with reference to our first C program.

int p, n ;/* declaration */
float r, si ;/* declaration */
si = p * n * r / 100 ; /* usage */
si = p * n * r / 100 ;

***** and / are the arithmetic operators. The arithmetic operators available in C are +, -, ***** and /. C is very rich in operators. There are as many as 45 operators available in C.

Surprisingly there is no operator for exponentiation… a slip, which can be forgiven considering the fact that C has been developed by an individual, not by a committee.

printf( ) and its Purpose

C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ). Let us understand this function with respect to our program.

printf ( "<format string>", <list of variables> ) ;

can contain, %f for printing real values %d for printing integer values %c for printing character values

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 printf( ) is executed.

    printf("%f\n", si);
    printf("%d %d %f %f\n", p, n, r, si);
    printf("Simple interest = Rs. %f\n", si);
    printf("Principal = %d\nRate = %f\n", p, r);

The output of the last statement would look like this…

Principal = 1000 Rate = 8.500000

What is ‘\n’ doing in this statement? It is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C. These are discussed in detail in Chapter 18. Right now, all that we

can say is ‘\n’ comes in handy when we want to format the output properly on separate lines.

printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c - d ) ;

Note that 3 and c also represent valid expressions.


Classes
Quiz
Videos
References
Books