Home » C programming language

Important points (rules) to remember while writing C/C++ program

If you want to become a good C/C++ programmer or want to write code like a professional programmer, here are some of the important points/rule that you should remember while writing a C/C++ program.
Submitted by IncludeHelp, on May 12, 2018

You should not place unnecessary comments to those statements which are already cleared.

For example:

/*The following statement will 
print age on the screen*/
printf("Age:  %d\n",age);

You should not use underscore (_) to start identifier name (like: variable, constant, etc.) unless it’s not required. It can be used to start with those variables which are unique or system related.

For example:

int _age; //not recommended
char* _name;  //not recommended

long _baudrate;  //recommended
unsigned  char  _portNo;   //recommended

Do not use an uninitialized variable; the output/result can be unpredictable.

For example:

//finding sum of all array elements
int sum;
for(i=0;  i<n; i++)
	sum += arr[i];

Here, sum is not initialized and we are using sum to add the array elements. So, there is no guarantee to get correct output.

You should not use semicolon (;) at the end of the function header. If you do this, compiler will through an error.

For example: (incorrect way)

int findSum (int x, int y);
	//body of the function 

Relation expressions evaluate to 0 or 1. Where, 0 is considered as false and 1 is considered as TRUE. But, if is there any expression or variable that you are using within a condition whose result is an integer value. It will be considered as True.

Note that:

  • Only 0 is considered as 'False'
  • Positive and negative integers are considered as 'True'

For example:

if (0) { ... }  //condition is false here
if(990) { ...}  //condition is true here
if ( -120 ) { ... }  // condition is true here

Do not use semicolon (;) at the end of the if statement. If you terminate if statement by the semicolon, then the statement written in the if statement body will always execute whether is true or not.

There will be two cases,

Case A ) if only if statement is there

For example:

int b=0;
if(b ! =0);
	result = a/b;

Here condition is true, still statement result = a/b; will be executed. Because compiler will consider two separate statement 1) if(b !=0); and 2) result =a/b;

Case B ) If there is ‘if else statement’

For example:

int b=0;
if(b != 0);
	resunt = a/b;

Here condition is true, but if statement terminated by semicolon. Then, compiler will generate an error due to else statement because else statement can only be used with if statement and here if statement is terminated by the semicolon.

Do remember to use ‘address of’ (&) operator while reading values from the scanf() function. Compiler will not generate error but output may unpredictable.

For example:

int num;
printf("Enter number: ");
scanf("%d",a); //invalid

Do remember that array index start with 0 (not from 1) and the index of last element is 'N-1'. Here, N is the total number of elements.

For example: If there are 10 elements in an array arr. Then the index of first element will be 0 and the last element’s index will be 9.

We can access the array elements by using its name and index like: arr[0], arr[1], ..., arr[9]

Since, you should not use semicolon after the loop statement. But, if you want to run a blank loop that should not execute any statement (a loop without its body statement), we can use loop statement with semicolon. Generally such types of looping statements are used to use delays between two statements.

For example:

int i ;

printf("Hello world!\n");
for(i=0; i<=10000; i++);

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.