Thursday 5 June 2014

The first C program



The first C program
Here is another version of first C program, rewritten with no extra spaces. Note that the comment has been eliminated.
#include<stdio.h>
#include<conio.h>
void main() { int n1, n2, sum, prod; printf (“\n Enter two numbers”); scanf(“%d%d”, &n1, &n2); sum=n1+n2; prod=n1*n2; printf (“\n Sum of two numbers = %d”, sum); printf (“\n Product of two numbers = %d ”, prod); getch(); }
Although the program is perfectly valid from the complier’s point of view, it is essentially unreadable by a person. So we have keep few things in mind while writing the code.

Programming style
A  C statement can appear anywhere on a line, but you should follow a consistent set of guidelines when typing your program. It will increase the readability of your program surely.

Rules:
1.We will start the first comment, describing what the program does, in column 1.
2.The lines specifying the include file(s) to be used with a program will also start in column 1, also the line containing main ( ).
3.The opening brace and the closing brace of the program will also be in column1.
4.Inside braces i.e. action portion of a program, we will indent all the statements using TAB key (up to eight spaces).
Program is given below:
 

No comments:

Post a Comment