Tip of the week#3: Use of ++ and —

++ and — are the unary operators for incrementing and decrementing the variables.

Let us take an example.

Example 1:

int i = 10;
i = i + 1;

Example 2:

int i = 10;
i++;

Although the above two example perform the same task, there is a significant difference (in ms) in the time required to execute the above two.

The execution of the instruction “i = i + 1” takes more time than “i++”.

Unary operators produce fewer instructions and run faster.

You may also like...

3 Responses

  1. Loveleen Sharma says:

    Awesome!
    I needed this difference.
    Can u plz elaborate it; the reason?
    I want to know that how does i++ takes fewer instructions as compared to i+1?
    Plz do reply within a day!
    Thanks!

  2. LearnCOnline says:

    For a give statement, there is a specific internal operations that needs to be processed by compiler.
    There is a difference between i = i + 1 and i++.

    In “i = i + 1”, first instruction (i + 1) would be executed and then the result is assigned to the variable i.

    In i++, the value of i itself gets incremented and there is no assignment operator involved. Hence i++ executes faster than i = i + 1
    Hope this clarifies.
    Thanks,
    LearnCOnline Team

  3. Loveleen Sharma says:

    Yeah this is relevant for me.
    Thanks for the clarification.
    Regards:-
    Loveleen Sharma.

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet