a little puzzle

This is a discussion on a little puzzle within the C++ Programming forums, part of the General Programming Boards category; here's an interesting little puzzle I found on another board: given the following sequence: int a=0, b=0, c=0, d=0; b ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    a little puzzle

    here's an interesting little puzzle I found on another board:

    given the following sequence:

    int a=0, b=0, c=0, d=0;
    b = ( a++, a++, 6+a );
    d = ( c++, 6+c, c++ );

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;

    fill in the expected output for each variable below:
    a =
    b =
    c =
    d =

    and explain your reasoning. Indicate any possible non-defined behaviors but assume the behaviors occur as expected.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    a = 2
    b = 8
    c = 2
    d = 1

    It's due to the comma operator....this operator evaluates the left, but then discards the value....only the furthest right value is assigned in this case.

    For "b = ( a++, a++, 6+a );" nothing is assigned from the 2 "a++" unary operations, but a is incremented each time. So the assignment is "b = 6+2" which is 8 and at that point, a is 2

    For "d = ( c++, 6+c, c++ ); " , c is first incremented, but the middle evaluation is discarded (and it doesnt change the value of c). So all that is assigned is "d = c++" and at that point, c has been incremented once (postfix increment operator does not take effect until after the assignment). So d is 1 and c is 2.

    Nice little puzzle....but if you understand the comma operator it's fairly easy to understand

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Excellent analysis and explanation, maybe even better than on the other board!

    I thought the little details in terms of the effect of the comma operator (I'd never seen a comma used like that before) and the difference in effect of the increment operator versus the addition operator on the variable it was associated with (all of which you explained wonderfully) was interesting.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thanks for clearing that up for me, Fordy.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's interesting... i've never seen that done before...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 12:53 PM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM

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