Thread: i++ and ++i

  1. #1
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30

    Unhappy i++ and ++i

    Anyone can tell me the different between this.....in programming c ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Test it.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    int i;
    int r;
    // test #1
    i = 7;
    r = ++i * 10;
    printf("%d %d \n",i,r);
    // test #2
    i = 7;
    r = i++ * 10;
    printf("%d %d \n",i,r);
    
    return 0; }
    Gives...
    8 80
    8 70

    Why two different answers?
    ++i increments i before the calculation
    i++ increments i after the calculation

    And, you wouldn't believe how many times I've tripped over this in various programs.
    Last edited by CommonTater; 09-23-2010 at 11:44 PM.

  3. #3
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30
    thank!!!

  4. #4
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30

    Lightbulb

    Anyone can solve this for me ....

    n=2
    n*=((3*4)/2)-9);

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We cannot. What do you think it produces, and why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The expression is malformed, having more right parenthesis, than left.

    As for solving it - what fun would that be? Write a little program to solve it.

    Big silly you! This forum is for programming, not math!

  7. #7
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30

    Unhappy

    i dont want in CODING .... if anyone can solve it in math and tell what it answer?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is BASIC math, part of general education!
    Don't tell me you don't know how to multiply, add and subtract?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30
    i know but i want to make sure the answer same with other people answer...can anyone answer this:
    n=2
    n*=((3*4)/2)-9);

    in math

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then post your answer to show that you actually have done it.
    Otherwise people will think you're just looking for an answer.
    And why not run it in a compiler and check the output?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30
    the answer is
    n = -6

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hakimstm1b
    i know but i want to make sure the answer same with other people answer...can anyone answer this:
    n=2
    n*=((3*4)/2)-9);
    Instead of reducing members of this community into pocket calculators, you could actually use a pocket calculator to double check. That said, this is the kind of thing that proves that you are not smarter than a fifth grader if you cannot compute it mentally.

    Quote Originally Posted by hakimstm1b
    the answer is
    n = -6
    That is correct. But are you sure that I am telling the truth? Really, what is stopping you from using a pocket calculator, or even just taking a pen and paper and working it out step by step? If you cannot trust yourself on something so simple when you work it out meticulously, you're doomed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Assuming n is a signed integer, I get the same answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30
    so you answer is -6

  15. #15
    Registered User hakimstm1b's Avatar
    Join Date
    Sep 2010
    Posts
    30

    Lightbulb

    thank

Popular pages Recent additions subscribe to a feed