Basic Rules For Writting C Program

Prashant | Sun, 07 Jun, 2020 | 180

Some Basic Rules for Writting C program

C program or any other programming language is the set of statements. And each line of code should follow the standard rules to make your code work properly.

In this article we are gonna tell you some basic rules of C programming which you need to follow in order to code like a professional programmer.

1) Comments/Documentation

Comments are not required in any of the program neither they are read by compilers but comments are so important to codes. So it si the first rule that you should remember to comment/document the code properly.

What you can document?

  • Purpose of the code.
  • Name of the programmer, reviewer, code writing, editing date etc.
  • Resources (inputs) that will be used to run the code.
  • Explain each logic, what is that for and why you used?
  • You can also write note for the developers, in which you can explain your logic/algorithm in details.
  • Each function declarartion definiton should be documented.
  • Anything that you can explain, but there is no need to write unnecessary comments with the code that is already cleared. 

2) Statement termination by semicolon (;)

Each statement which does not has its body (please note that, statement without its body) must be terminated by the semicolon (;).

The statements which should be terminated

  • All printf(), scanf() or any other function calls.
  • All declarations like variables, constants, function, structures must be terminated by semicolon.
  • All expressions must be terminated by the semicolon (;).

Examples:

int a; //variable declarations

printf("Hello!"); // a print statement 

sum = findSum (10,20,30); // a function calling statement

he statements which should not be terminated

  • Header files include statements.
  • Macro definition statements.
  • If statement, loop statements, function header with the function definitions.
#include <stdio.h> //hesder file include section

#define MAX_LEN 1024 //Macro definition statement

int main() //main function defining section
{
	//...
}

3) Tokens, identifiers related rules

You must be aware about the C language tokens to differentiate what is keyword, what is identifier etc.

Some of the points that you must know:

  • Keywords are the reserved words in the compilers and you cannot use them for other purposes.
  • Same as keyword, library functions are also declared in the header files and you must have to include header file in the program. For example, if you are using sqrt() function to find the square root of any number, you will have to include math.h header file.
  • C language is cases sensitive programming language. Here, small case identifiers and uppercase identifiers are different. So, if you have declared a variable with name Num then you must have to use Num everywhere. You cannot use num, NUM etc instead of Num.

4) Rules about identifier/variable declarations

  • Lowercase, uppercase alphabets, digits and underscore (_) are allowed to define an identifier (i.e. variable name, constant name, function name etc.).
  • Identifier must be meaningful and descriptive.
  • Only an alphabet or underscore (_) can be used as first letter of the identifier.
  • White spaces and other special characters are not allowed in an identifier name, if you have two words in a variables/identifier name, you can use either underscore (_) to separate them or you can also write identifier name in camel case style.
  • For example: if you want to declare a variable to store roll number, the variable name should be roll_number or rollNumber or anything like that but you cannot use white spaces.

5) Code indentation

I have deal with many programmers who just write the code and they don’t care about the indentation, since indentation is not compulsory for the compiler. But it’s most important while writing the program. An indented code can read, edit easily.

 

 

 

 

 

 

Related Articles :- 

1Introduction And Getting Started With C

2Basic Of C Language

3. Why We Should Use C Language

4. Applications Of C Programming

5. Basic Rules For Writting C Program

6. Keywords In C Language

7. Comments In C Programming

8. Tokens In C

9. Difference Between Int Main And Void Main

10. C Processor And Macros

11. Variables In C

12. Types Of Variables In C

13. Difference Between Local And Global Variable

14. Difference Bwtween Auto / Extern / Static Variable

15. Constant In C

16. How To Access Global Variable Using Extern Keyword In C

17. Exit And Return Staterment In C

18. Print Float Value Upto N Decimals In C Programming

19. How To Print Multiline Message Using Single Printf In C Programming ?

20. What Value Returned By Scanf Function In C Language ?

21. What Value Is Returned By Printf And Scanf In C

22. What Do 'Lvalue' And 'Rvalue' Mean In C/C++?

23. Errors In C Programming ?

0 comments
Leave a comment