×

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

C programming optimization techniques

In this tutorial, we will learn about the some of the top/most useful C programming language optimization techniques that help you to sharp your programming skills. By IncludeHelp Last updated : April 20, 2023

As we all know that C programming is a widely used and most popular programming language and it is general purpose programming language which is used to develop any type of applications like low level and high level.

So, if you are learning C programming language or if you are a C language programmer, you must read these C programming optimization techniques and use all given techniques to improve your programming skills.

C language optimization tips/techniques

1. main() function should be used properly by using return type and command line arguments

Correct forms:

int main(int argc, char* argv[])
int main (int argc, char** argv)

Read: main() in c

2. Each variable should be declared in separate line, with sufficient spaces

While declaring variables, remember two things

  1. Declare each variable in a separate line - it will help to put inline (single line comment after the variable declaration).
  2. Initialize variable with default values while declaration - it will help to prevent unpredictable behavior of the variable (variable may give unpredictable result if it is using without initialization).

There should be one tab space between data type, variable name and initialization (variable initialization) value.

Correct declaration:

int height = 0; //height of the object
int weight = 0; //width of the object
float result = 0.0f; //result of the expression
char choice = 'N'; //store user's choice

3. There is no need to use integer data type all time for small values

There are other data types, which are used to store small integer values, see the below given table and use according to the variable range.

Data type Required memory Value range
char (signed char) 1 byte (8 bits) -128 to 127
unsigned char 1 byte (8 bits) 0 to 255
short (signed short) 2 bytes (16 bits) -32768 to 32767
unsigned short 2 bytes (16 bits) O to 65535

If a variable's value is positive in all cases, you may use ‘unsigned' with the data type, it ensure that only positive values will be stored.

For example: If you are going to declare a variable to store age of a person and we know that minimum age is 0 and maximum age is 100 or 150 (which, is not possible still we can consider maximum age is 150).

Then, the following declaration is correct for it,

unsigned char   age =   0;

Note: for an unsigned type of variable, make sure its value must be zero or positive, if there is any negative value, value will be truncated.

4. Do not declare more global variables

Global variable's scope is life time of the program that means if there is any variable declared in global scope it can be used in all functions and it will live till program's execution.

Therefore, try to declare variables inside the function blocks; they will de-allocate/destroy when program's execution leaves the function.

5. Use constant or macro to declare a static array

If there are multiple declarations of arrays with the fixed values, it will hard to update/change (in future). We can use Macro or constant to declare an array.

Incorrect method:

int main()
{
    char first_name[100];
    char second_name[100];
    //...
}

Correct method 1:

#define MAX_CHAR 100
int main()
{
    char first_name[MAX_CHAR];
    char second_name[MAX_CHAR];
    //...
}

Correct method 2:

const unsigned char MAX_CHAR = 100;

int main()
{
    char first_name[MAX_CHAR];
    char second_name[MAX_CHAR];
    //...
}

6. Use switch statement instead of if else to check integral values

If you are writing code to check integral values (like integers, characters), it is recommended that you should use switch statement rather than if else statements.

In switch case statement program's execution jumps to matched case and execute the statement written in that case.

While, in if else statement, each condition (one by one) is checked till the true condition.

Incorrect method:

if (choice == 1) {
    //statements
}
else if (choice == 2) {
    //statements
}
else if (choice == 3) {
    //statements
}
... else
{
    //default statement
}

Correct method:

switch (choice) {
case 1:
    //statements
    break;

case 3:
    //statememts
    break;

    ...

        default :
        //default statements
        [break;
    ] //optional
}

In such cases, switch statements may fast.

How?

Let suppose, if last case (condition) required, if statement will reach there by checking all above conditions, while switch statement will directly jump to that case and execute the statements. Therefore, second method will be faster.

7. Conditions breakdown

If there are multiple conditions and you are checking them by using if else statement, you may break down them, by using parent and child conditions.

Consider the given example:

if (option == 1) {
}
else if (option == 2) {
}
else if (option == 3) {
}
else if (option == 4) {
}
else if (option == 5) {
}
else if (option == 6) {
}
else if (option == 7) {
}

We can breakdown the conditions in multiple parts like,

if (option <= 4) {
    if (option == 1) {
    }
    else if (option == 2) {
    }
    else if (option == 3) {
    }
    else if (option == 4) {
    }
}
else {
    else if (option == 5)
    {
    }
    else if (option == 6)
    {
    }
    else if (option == 7)
    {
    }
}

In such case, conditions check will be less and program's performance will be faster.

Read more...

I hope this post will help you to improve your programming skill, if you have any C programming optimization techniques, please write in the comment box.




Comments and Discussions!

Load comments ↻





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