c++ questions

This is a discussion on c++ questions within the C++ Programming forums, part of the General Programming Boards category; i have a few c++ questions. i will write down what the question is and my answer. i am just ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    12

    c++ questions

    i have a few c++ questions. i will write down what the question is and my answer. i am just looking to see if i am correct.

    int I = 20;
    int k = 15;
    int j = 5;
    j = k; my answer is j = 15
    k = j; my answer is k = 5
    j = I; my answer is j = 20

    double x = 31.2;
    double y = 43.2;
    double z = 0.0;
    x = z; my answer is x = 0.0
    y = z; my answer is y = 0.0
    x = y; my answer is x = 43.2

    int a = 1;
    int b = 2;
    a = b++; my answer is a = 2 and b++ = 2

    a = ++b; my answer is a = 3 and ++b = 3

    are these correct?

    thanks

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by jmarsh56
    Code:
    int I = 20;
    int k = 15;
    int j = 5;
    j = k; my answer is j = 15
    k = j; my answer is k = 5
    j = I; my answer is j = 20
    I would say you are wrong.
    After the first statement "j" has the value of 15.
    Now the second statement "k" has the value 15 because "j" just changed in the above statement.
    Last "j" has the value 20.
    Quote Originally Posted by jmarsh56
    Code:
    double x = 31.2;
    double y = 43.2;
    double z = 0.0;
    x = z; my answer is x = 0.0
    y = z; my answer is y = 0.0
    x = y; my answer is x = 43.2
    The last answer is wrong because the previous statement change the value of "y" to 0.0. So for the last one "x" has the value 0.0

    Quote Originally Posted by jmarsh56
    Code:
    int a = 1;
    int b = 2;
    a = b++; my answer is a = 2 and b++ = 2
    
    a = ++b; my answer is a = 3 and ++b = 3
    These are wrong as well.
    "a" does have the value 2 but "b" has the value 3 after the statement finishes.
    Now for the second one "b" has the 3 but it is incremented before it is used so it now has the value 4. So "a" has the value 4 and so does "b".

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The thing to remember is:

    The value on the right is assigned to the variable on the left.



    These things with equal-signs that look like mathematical equations are NOT equations. They are assignment statements.

    In just about any programming language, you can write X = X+1. You cannot do that in math!

    You can only have one variable on the left*. On the right, you can have a constant, “known” variable, or any expression that computes to a value.

    Sometimes X++ and ++X are the same thing. It’s just a matter of precedence (which operation is done first). Look-up prefix and postfix. For example, prefix has higher precedence than the assignment operator (the equal sign). Postfix has lower precedence than the assignment operator.

    FYI - In most other situations, you can forget about precedence and use parenthesis to force the desired order of operation. It makes your program easier to read, and helps avoid bugs!


    * The variable on the left is often called an L-value. If you get an error message that says something about an L-value (or lvalue), it usually means you did something like 5 = x+1, which is invalid in C/C++, because you cannot assign a new value to 5!
    Last edited by DougDbug; 05-02-2006 at 07:11 PM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    thanks for the help now i am starting to understand this more

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21