C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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. By IncludeHelp Last updated : December 15, 2023

1. Do not place unnecessary comments

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);

2. Do not use underscore with variables & constants

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

3. Do not use uninitialized variables

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.

4. Do not use semicolons everywhere

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 

5. Be careful while using relation operations

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

6. Be careful while writing conditional statements

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.

7. Use address of operator while using scanf()

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

8. Be careful with array indexing

Do remember that C 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]

9. Use blank loops where required

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++);

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.