Thread: calculation

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    14

    calculation

    Code:
    #include<stdio.h>
    main()
    {
       int a=1;
       printf("%d\n",a+=(a+=3,5,a));
    }
    What happens in the printf statement to print 8 !!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Expressions
    Learn about sequence points, and the "modified at most once" rule.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    @Salem...as you said i went through those concepts...and now i get a bit of those....but im not getting how the expression "
    a+=(a+=3,5,a)
    " is sequenced for evaluation ??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How many times is a being modified between one ; and the next ; (otherwise known as sequence points).
    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.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    I am reallly confused.....im only able to make some guess by trying to modify the code in different ways...each time gettin different answers.....as i dont want to put up here my guesswork and be really specific...would you ease me with 'how' and 'what' happens in the statement "
    a+=(a+=3,5,a) "

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    My answer would be to write proper code which doesn't invoke UB and get on with life.

    These silly assignments by clueless teachers are not worth the effort (yours or mine).

    The simple answer is to run the code through a number of compilers, take what appears to be the most "popular" answer, and submit that.
    If your clueless teacher wants to argue over it, then show them ALL your results and then refer them to the comp.lang.c FAQ I posted.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    We get so flooded with these stupid undefined behavior questions. I continue to await one of these Indian students (as that's where these assignments appear to come from) telling us whether they are being taught this as what not to do (because it's undefined behavior) or if they are actually being taught this is valid code. Unfortunately, as a rule, these posters are one-hit wonders.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    All write guys thanks anyways for your concern.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    @rags....that was an understatement.
    xyz88

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by rags_to_riches View Post
    We get so flooded with these stupid undefined behavior questions. I continue to await one of these Indian students (as that's where these assignments appear to come from) telling us whether they are being taught this as what not to do (because it's undefined behavior) or if they are actually being taught this is valid code. Unfortunately, as a rule, these posters are one-hit wonders.
    I had a Computer Exam a few days ago... and some of the code in the paper was so utterly horrible that I just wish I had the gall to draw a big troll (Problem Bro? ) shouting the possible problems with those codes .... in the answer script.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculation always gives 0
    By Emma K in forum C Programming
    Replies: 18
    Last Post: 11-24-2010, 06:00 PM
  2. Help With Calculation
    By WackoWolf in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2005, 05:18 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  4. calculation
    By coo_pal in forum C Programming
    Replies: 2
    Last Post: 02-09-2003, 11:41 PM
  5. Calculation Help
    By reddtee in forum C Programming
    Replies: 19
    Last Post: 11-24-2001, 04:17 PM

Tags for this Thread