Am I understanding this C code correctly ???

This is a discussion on Am I understanding this C code correctly ??? within the C Programming forums, part of the General Programming Boards category; int a = 1, b = 2, c = 3; a += b += c += 7 Interpreting what I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    Am I understanding this C code correctly ???

    int a = 1, b = 2, c = 3;
    a += b += c += 7

    Interpreting what I read on page 64 in the same text, I derive the following:

    c = c + 7, which now means that c = 10 = 3 + 7, followed by
    b = b + c, which now means that b = 12 = 10 + 2, followed by
    a = a + b, which now means that a = 13 = 12 + 1

    Am I on target?

    If not, would you steer me in the right direction?

    Thx, Daniel
    Last edited by AntonsonDJ; 05-08-2005 at 07:45 PM. Reason: clarification of my msg

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Am I on target?
    Yes, you are. Of course it's probably easier to put that code in a compiler and run it, then post it on this forum and wait for an answer.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > Of course it's probably easier to put that code in a compiler and run it
    True, but had the expression been one of those stupid i++ + ++i type things which seem to crop up from time to time, then they would have been wise to post the question.

    Trying and compiling is also good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    and here is a sample code of how do they work when u implement it

    Code:
    #include<stdio.h>
    
    int main()
    {
            int a=10,b=20,c=30;
            int result;
            
            a+=b+=c+=10;
            
            printf("Result: %d\n",a);
            a=10,b=20,c=30;
            c = c + 10;
            b = b + c;
            a = a + b;
    
            printf("Result: %d",a);        
            
            getchar();
            return 0;
    }
    and my output
    Code:
    Result: 70
    Result: 70
    s.s.harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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