Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By rags_to_riches
  • 1 Post By manasij7479

calculation

This is a discussion on calculation within the C Programming forums, part of the General Programming Boards category; 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 !!!...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    rags_to_riches likes this.
    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.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    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.
    CommonTater likes this.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    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.
    rags_to_riches likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 06:18 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 05: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


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