The First C Program

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. Before we begin with our first C program do remember the following rules that are applicable to all C programs:

  • Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements.
  • 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.
  • 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.
  • All statements are entered in small case letters.
  • 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.
  • Every C statement must end with a ;. Thus ; acts as a statement terminator.

First C Program:

Write a program to simply calculate simple interest for a set of values representing principle, number of years and rate of interest.

/* Calculation of simple interest */
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 see few things in the program

  • Comment about the program should be enclosed within /* */. For example, the first 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 as shown below:

/* formula */ si = p * n * r / 100 ;
si = p * n * r / 100 ; /* formula */
si = p * n * r / /* formula */ 100 ;

  • Sometimes it is not so obvious as to what a particular statement in a program accomplishes. At such times it is worthwhile mentioning the purpose of the statement (or a set of statements) using a comment. For example:

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

  • 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.
  • Although a lot of comments are probably not necessary in this program, it is usually the case that programmers tend to use too few comments rather than too many. An adequate number of comments can save hours of misery and suffering when you later try to figure out what the program does.
  • The normal language rules do not apply to text written within /* .. */. Thus we can type this text in small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and are completely ignored by the compiler.
  • Comments cannot be nested. For example:

/* Cal of SI /* Author sam date 01/01/2002 */ */
is invalid.

  • A comment can be split over more than one line, Such a comment is often called a multi-line comment. For Example:

/* This is

a jazzy
comment */

  • main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below.

main( )
{
statement 1 ;
statement 2 ;
}

  • 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. For example:

int p, n ;

float r, si ;

  • Any C statement always ends with a ; For example:

float r, si ;

r = 8.5 ;

  • In the above program * 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. 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. For example:

si = p * n * r / 100 ;

  • 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 readymade library functions. One such function is printf( ). We have used it display on the screen the value contained in si.

Printf()  : The Printf Function

The general form of printf( ) function is,

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

<format string> can contain, %f for printing real values,  %d for printing integer values and  %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 the printf( ) is executed. Following are some examples of usage of printf( ) function:

printf ( “%f”, si ) ;
printf ( “%d %d %f %f”, p, n, r, si ) ;
printf ( “Simple interest = Rs. %f”, si ) ;
printf ( “Prin = %d \nRate = %f”, p, r ) ;

The output of the last statement would look like this…

Prin = 1000
Rate = 8.5

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.

Right now, all that we can say is ‘\n’ comes in handy when we want to format the output properly on separate lines. 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, 3, 3 + 2, c and a + b * c – d all are valid expressions. The results of these expressions can be printed as shown below:

printf ( “%d %d %d %d”, 3, 3 + 2, c, a + b * c – d ) ;

Note that 3 and c also represent valid expressions.

 

Related Posts

  • 37
    In the First C Program discussed earlier (same has been also mentioned below for the reference) we assumed the values of p, n and r to be 1000, 3 and 8.5. Every time we run the program we would get the same value for simple interest. If we want to calculate…
    Tags: program, values, printf, interest, simple, function, set, si
  • 34
    It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from centigrade to Fahrenheit for 15 different cities. Let us look at a example which…
    Tags: si, printf, interest, simple, main, values, calculate, example

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: