Thread: Am I understanding this C code correctly ???

  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,268
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    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, 04: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-16-2002, 12:04 AM