Writing a ‘C’ program C

  • Statements.
  • Comments.

Now let’s put above knowledge to use, by writing our first ‘C’ program.

Each instruction of a ‘C’ program is called a statement. Each statement has to have a ‘;’ (semicolon) at the end. Semi colon is the program terminator.
More than one statement enclosed in brace brackets is called compound statement.
Compound statements are often referred as block of statements. If it has to be a compound statement, brace brackets around all the statements in the compound statement is compulsory. Brace bracket enclosing single statement is optional.

(we will study if-else statements in next chapter)
We have acquired sufficient knowledge so as to write a simple ‘C’ program. The structure of any ‘C’ program is as follows.

Let us write a small ‘C’ program to calculate simple interest.

You will type this program into an editor. Since we will use the Turbo C compiler, we will write the program in the Turbo ‘C’ editor using the ‘New’ submenu in the ‘File’ menu. Every “C” program should begin with main (); which is a function. Function ‘main’ should be followed by (). All the statements are enclosed in brace brackets. The statements between opening and closing braces are called the body of the program.
We have defined two variables ‘p’ and ‘n’ as integers and two variables ‘r’ and ‘si’ as float. We have assigned a value of 1000 to p’, 3 to ‘n’ and 8.5 to ‘r’ using the assignment operator ‘=’.

si = p * n * r /100 is an arithmetic expression. Here expression on right of ‘=’ is solved, according to
precedence and associativity and type conversion rules and resulting value is converted to float and assign to ‘s’, since s is a floating point variable. In the end we have used printf function to display the value of ‘si’.
The #include<stdio.h> statement is called the preprocessor directive. All preprocessor directives are written before ‘main’. The file ‘stdio.h’ contains all information for using library function ‘printf. Since we are using ‘printf’ in our program we will need to include it in the program. This is done by using the preprocessor directive #include’.

Comments are lines written by programmer to give additional information or logic of the program. A line enclosed within /* and */ is a comment. Any number of comment can be placed anywhere in the program. A comment can be split over more than one line.

/* This is
a jazzy
comment */

Comment cannot be nested.

/* cal of si / This is not allowed */*/

It should be kept in mind that comments are needed since if some other programmer has to work on our program or if we ourselves have to work on it after a long time, they will be helpful. Over commenting should be avoided.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics