C Program: Basic Rules
Hi,
The read time for this post is approx 7 mins.
Rules for writing C programs:
For Example:
I will rewrite our first program i.e. Hello World program here to explain rules further.
Rule 1: The purpose of this program is to print Hello World for this to happen, the compiler needs some header files in mentioned sequence and then, a main() function which is the entry point for execution followed by printf statement and closing the braces denotes exit point.
Rule 2: In this program there are no jumping or repetitive statements, its just a simple sequential program.
Rule 3: Here, to increase readability, blank spaces can be given for better readability. In that case the program would look like.
Observe the spaces after #include and printf.
Rule 4: All the statements are already in small case.
Rule 5: Check the position of printf statement in above program, there is a tab before it. This tab be removed without affecting flow of program. Whereas in python, every space and tab matters a lot as the blocks are created on basis of tabs and spaces.
Rule 6: All the statements are ended with ";", it is mandatory to end every statement with semi-colon.
This is all with Rules of writing C program.
In the next post we will write a Simple Interest Calculator program with comments and formula.
Thanks
Prateek
In this post I will post basic C program writing rules. No need to remember these rules, as on one you will get habitual to them as you write more and more programs on other hand if you wont follow these rules, the compiler is most likely to give you error at time of compilation.
The read time for this post is approx 7 mins.
Rules for writing C programs:
- Each program in program is written as a separate statement, and complete C program consists of sequence of statements.
- "C" program follows the same sequence of execution as written in instructions untill and unless the control is transferred to other section of program through control statements. if...else, do...while, while, switch...case, for etc are control statements.
- For better readability, blankspaces are inserted between words, however, blankspaces are not allowed in naming of constants, variables or keywords.
- All statements should be entered in small case only.
- C is called free form language, as there is no rule regarding position of statement, but logical sequence is important.
- Every C statement is terminated by a semi-colon i.e ";"
For Example:
I will rewrite our first program i.e. Hello World program here to explain rules further.
#include<stdio.h> //Line 1
#include<conio.h> //Line 2
void main() //Line 3
{
printf("Hello World"); ////Line 4
}
Rule 1: The purpose of this program is to print Hello World for this to happen, the compiler needs some header files in mentioned sequence and then, a main() function which is the entry point for execution followed by printf statement and closing the braces denotes exit point.
Rule 2: In this program there are no jumping or repetitive statements, its just a simple sequential program.
Rule 3: Here, to increase readability, blank spaces can be given for better readability. In that case the program would look like.
#include <stdio.h>
#include <conio.h>
void main()
{
printf ("Hello World");
}
Observe the spaces after #include and printf.
Rule 4: All the statements are already in small case.
Rule 5: Check the position of printf statement in above program, there is a tab before it. This tab be removed without affecting flow of program. Whereas in python, every space and tab matters a lot as the blocks are created on basis of tabs and spaces.
Rule 6: All the statements are ended with ";", it is mandatory to end every statement with semi-colon.
This is all with Rules of writing C program.
In the next post we will write a Simple Interest Calculator program with comments and formula.
Thanks
Prateek
Comments
Post a Comment