In the previous articles, I already told you about
variables, constants, and keywords, etc. Now the next step is to combine them
(variable, keywords, and constants) to form an instruction. But before knowing
about instruction we are going to make a new program using variable constant
and keywords.
But before going to make a program we have to study the
procedure or rules of writing a program in c language. Remember that these rules are applicable to
all c programs written on c language. Let’s Begin...
- Every C program statement must be ended with “;” (semicolon) generally known as a statement terminator.
- All the statements should be written in small letters.
- Blank space is used between two words to improve the statement readability.
- No blanks were allowed between the variables, constants, and keywords.
- Each Instruction in c program is written as a separate statement.
Let us write down our first program in c language. In this program
we are going to calculate the simple interest of any amount.
/*
Calculation of simple interest */
/* Author
Mad About Computer Date: 01/09/2020 */
main( )
{
int p, t ;
float r, si ;
p = 1000 ;
t = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * t * r / 100 ;
printf ( "%f" , si ) ;
}
Explanation
of the program.
- The first two lines of the program are a comment. The Information provided between the /* */ is the information of the program, programmer, and date of the program when it was written.
- It is not necessary to write down the comment in your program but it is a good practice of the programmer to begin the program with a comment which shows the information about the programs and something else which is important in your program.
- After the comment, we used a function called main (). Main() is the function of c program. It indicates where your program has started. When you execute your program it directly executes from the main functions.
- After the main function, we declared our variables where p represents the principle, t represents time, the r represent rate and si represent the simple interest respectively. It is necessary to declare the variable before we use it on the program. We already assign the value of the variables. ( p = 1000, t = 3.5, r = 8.5 ).
- After the variable declaration, we have to put the formula of the simple interest into the program. The formula of the simple interest is p*r*t/100. In the formula, you have seen we use (*, / ) symbol. What does it mean? The symbol is known as an arithmetic operator. We discuss the operator in detail later. Just know that * operator is used to multiplying two or more values. And the / operator is used to divide two or more values.
- Once the s.i value Is calculated we have to display it on the screen. To display the result of the simple interest, we have to use printf() keyword on the program. Printf() keyword is used to display anything on the display screen
- %f for printing real values
- %d for printing integer values
- %c for printing character values
In this tutorial, we learn about principle or rules for writing c programs in C programming. If you find any mistakes on it. Or if you have any quarry related to it please comment below. Follow us for more programming tutorials.
You May Also Like