Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

Some basic rules of writing a C program



In this article, I am going to list some of the basic rules of writing a C program. I would strongly recommend following all these rules to code like a professional programmer.
Submitted by IncludeHelp, on May 13, 2018

Some basic rules of writing a C program

C program is the set of statement written in C programming language. Each program should follow the standards (rules) to make your code working everywhere.

1) Comments/Documentation

Comments are not required, but the comments are as important as code. So it is first rule that you should remember to comment/document the code properly. Read more: How to comment the code?

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 declaration, definition should be documented.
  • Anything that you can explain, but there is no need to write unnecessary comments with the code that is already cleared. (Read more: Important rules to remember while writing C, C++ program).

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

//etc

The 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
{
	//...
}

//function header with its definition
int findSum(int a, int b, int c) 
{
	return (a+b+c);
}

if(a > b) //if statement 
	large =a;
else
	large =b;

//etc

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.
  • 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.
  • 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.

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.).
  • 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.
  • Identifier must be meaningful and descriptive.

Read more: Identifier/variable name conventions in C language



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.

Example of indented code:

#include <stdio.h>

int main()
{
	int a;
	int b;
	int large;
	
	a = 10;
	b = 20;
	
	if(a>b)
	{
		large = a;
	}
	else
	{
		large = b;
	}
	
	printf("Largest number is: %d\n",large);
	
	return 0;
}

Example of un-indented code:

#include <stdio.h>

int main()
{
int a;
int b;
int large;

a = 10;
b = 20;

if(a>b)
{
large = a;
}
else
{
large = b;
}

printf("Largest number is: %d\n",large);

return 0;
}





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.