Results 1 to 7 of 7

Thread: Comparing the post increment and pre increment operators in C Programming

  1. #1
    Join Date
    Dec 2009
    Posts
    66

    Comparing the post increment and pre increment operators in C Programming

    Please tell me which amongst the pre increment operator and the post increment operator is faster in C programming or both of them are equally fast ? please try to explain me the reason behind it as I am preparing to give an Interview in a software Company and it is one of the common question being asked by the interviewer ?
    Last edited by Hebrew; 15-12-2009 at 07:16 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Comparing the post increment and pre increment operators in C Programming

    According to my Knowledge the pre increment operator is much more efficient and faster than the post increment operator. since, the post increment operator object increments itself and after that it returns a temporary object containing its old value. This is true for even built in data types. Due to this reason the pre increment operator is faster than the post increment operator.

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Comparing the post increment and pre increment operators in C Programming

    Both the post increment and pre increment operators doesn't have much difference in timing. Although if we use pre increment in a for loop it proves to be quite faster. In both the operations the operand is incremented. But, the post increment operator firsts make a copy of the old value and then increment and returns the old value. Whereas the pre increment operator just increments and simply returns the value.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Comparing the post increment and pre increment operators in C Programming

    To explain this I would like to give you a short Example

    i=5; the value of i is 5 on this line
    i++; the value of i=5 on this line
    ++i; the value of i is 7 on this line

    from the above example we can easily understand that pre increment operator increments the value of i on the line itself whereas, the post increment operator increments the value of i on the second line.
    Last edited by Reegan; 15-12-2009 at 08:11 PM.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Comparing the post increment and pre increment operators in C Programming

    Every time when the post increment operator is used it performs two tasks firstly it stores the value of the variable in temporary location suppose in any of the register and then it increments the value of the variable and save it to the memory location. The incremented value is then transferred from the register to wherever it was assigned. Apart from that the pre increment does not have to transfer the value That's why it is quite faster than the post increment operator

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Comparing the post increment and pre increment operators in C Programming

    We cannot say it for sure since all that depends on the target processors. let us consider a few processors
    1.Sun's SPARC processor: This processor always executes that instructions that follows a conditional branch, without taking care of the condition inside the branch.
    2.GCC code generator:This hides the increment statement which helps in speeding of the post increment instruction.
    So, Its quite obvious that different processor implements different tricks for executing an increment statement faster than the other statement.

  7. #7
    jeryruse Guest

    Re: Comparing the post increment and pre increment operators in C Programming

    Hello Hebrew.
    They both increment the variable. But the value returned by the pre-increment operator is the value of the variable after it has been incremented, while the value returned by the post-increment operator is the value before it has been incremented.
    For example:
    int a = 1;
    int b = ++a;
    // Now a is 2 and b is also 2.

    int a = 1;
    int b = a++;
    // Now a is 2 but b is 1.
    Thanks for sharing it.

Similar Threads

  1. Need help for Acrobat Increment Java Code
    By RaaginiO in forum Windows Software
    Replies: 10
    Last Post: 13-11-2011, 04:19 PM
  2. Max energy increment in FrontierVille
    By Raja Ram in forum Video Games
    Replies: 7
    Last Post: 17-03-2011, 10:23 PM
  3. PHP : Increment date in mysql
    By Warner in forum Software Development
    Replies: 3
    Last Post: 02-07-2009, 06:23 PM
  4. Php variable increment problem
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 07:39 PM
  5. [Excel] no increment date
    By Florian in forum Windows Software
    Replies: 3
    Last Post: 15-10-2008, 05:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,984,682.93045 seconds with 17 queries