About += and ++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s0suk3@gmail.com

    About += and ++

    I'm new to C and having doubt about the += and ++ operators. Writing
    'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++', but
    the fetched value will be different). But since 'i += 1' is rather
    equivalent to 'i = i + 1', then it is a reassignment, right? So, is
    writing '++i' or 'i++' also a reassignment? What is the compiler doing
    internally to increment the value in that case? Or is that
    implementation-defined behavior?

    Thanks,
    Sebastian
  • Richard Tobin

    #2
    Re: About += and ++

    In article <488aa8c2-c95d-45b2-a5cf-7f15d9595918@d4 5g2000hsc.googl egroups.com>,
    <s0suk3@gmail.c omwrote:
    >I'm new to C and having doubt about the += and ++ operators. Writing
    >'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++', but
    >the fetched value will be different). But since 'i += 1' is rather
    >equivalent to 'i = i + 1', then it is a reassignment, right? So, is
    >writing '++i' or 'i++' also a reassignment?
    Yes, all of them assign new values to i. As statements, i+=1, i++,
    and ++i, are all equivalent to i=i+1, and the compiler can (and
    probably will) implement them all identically.

    As you note, i++ is different when used within another expression.

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

    Comment

    • Chris Dollin

      #3
      Re: About += and ++

      s0suk3@gmail.co m wrote:
      I'm new to C and having doubt about the += and ++ operators. Writing
      'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++', but
      the fetched value will be different).
      Yes.
      But since 'i += 1' is rather equivalent to 'i = i + 1', then it
      is a reassignment, right?
      Well, it's an assignment; nothing special about it, unless you
      mean something special. Do you?
      So, is writing '++i' or 'i++' also a reassignment?
      It updates `i` -- it assigns it a new value. If that qualifies
      as a reassignment, yes.
      What is the compiler doing internally to increment the value
      in that case?
      It's generating code to increment the variable.
      Or is that implementation-defined behavior?
      No; it's implementation-specific. ("Implementatio n-defined" is a
      technical term of the C standard, meaning "the implementation must
      do something about X /and tell you what it does/". Implementation-
      specific isn't a term of the Standard; I just mean that the
      implementation does whatever it does and isn't obliged to tell you
      what that is.)

      Some machines (eg PDP-11) have fancy "increment" instructions
      which will add 1 to a register (or even a memory location).
      Other machines have to use their ordinary "add" instruction with
      an operand of literal-one (eg ARM, in regsisters). Yet other
      machines might need to use an add-register instruction after
      loading one of the registers with the value 1.

      It All Depends.

      --
      "The wizard seemed quite willing when I talked to him." /Howl's Moving Castle/

      Hewlett-Packard Limited Cain Road, Bracknell, registered no:
      registered office: Berks RG12 1HN 690597 England

      Comment

      • santosh

        #4
        Re: About += and ++

        s0suk3@gmail.co m wrote:
        I'm new to C and having doubt about the += and ++ operators. Writing
        'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++', but
        the fetched value will be different).
        Yes. But the += notation allows you to add any arbitrary value to 'i'.
        The increment operators always add one.
        But since 'i += 1' is rather
        equivalent to 'i = i + 1', then it is a reassignment, right? So, is
        writing '++i' or 'i++' also a reassignment?
        In an abstract sense yes. But the compiler may or may not emit the same
        instructions for i += 1 versus ++i.
        What is the compiler doing
        internally to increment the value in that case? Or is that
        implementation-defined behavior?
        It's completely implementation defined as to the correspondance between
        these statements and their object code instructions. The increment and
        decrement operators were introduced so that some implementations could
        take advantage of more efficient instructions (INC and DEC) for them in
        comparison to a generic ADD or SUB instruction. What actually happens
        depends on the characteristics of your system and your compiler's
        optimisation settings.

        Comment

        • santosh

          #5
          Re: About += and ++

          santosh wrote:
          s0suk3@gmail.co m wrote:
          >
          >I'm new to C and having doubt about the += and ++ operators. Writing
          >'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++',
          >but the fetched value will be different).
          >
          Yes. But the += notation allows you to add any arbitrary value to 'i'.
          The increment operators always add one.
          >
          >But since 'i += 1' is rather
          >equivalent to 'i = i + 1', then it is a reassignment, right? So, is
          >writing '++i' or 'i++' also a reassignment?
          >
          In an abstract sense yes. But the compiler may or may not emit the
          same instructions for i += 1 versus ++i.
          >
          >What is the compiler doing
          >internally to increment the value in that case? Or is that
          >implementati on-defined behavior?
          >
          It's completely implementation defined as to the correspondance
          between these statements and their object code instructions.
          Well, having seen Chris Dollin's post I feel obliged to point out that
          my use of the term "implementa tion defined" above is incorrect. See
          Chris's post for details.

          <snip rest>

          Comment

          • Antoninus Twink

            #6
            Re: About += and ++

            On 13 Jun 2008 at 10:27, Chris Dollin wrote:
            s0suk3@gmail.co m wrote:
            >What is the compiler doing internally to increment the value
            >in that case?
            >
            Some machines (eg PDP-11) have fancy "increment" instructions
            which will add 1 to a register (or even a memory location).
            Some rather common machines have fancy "increment" instructions, but
            nonetheless the manufacturer advises optimizing compiler writers to use
            add *,0x1 instead...

            Extract from <http://www.intel.com/design/processor/manuals/248966.pdf>:

            Assembly/Compiler Coding Rule 32. INC and DEC instructions should be
            replaced with ADD or SUB instructions, because ADD and SUB overwrite all
            flags, whereas INC and DEC do not, therefore creating false dependencies
            on earlier instructions that set the flags.

            Comment

            • Antoninus Twink

              #7
              Re: About += and ++

              On 13 Jun 2008 at 10:27, santosh wrote:
              But the compiler may or may not emit the same instructions for i += 1
              versus ++i.
              Can you name a compiler that doesn't?

              Comment

              • Richard Tobin

                #8
                Re: About += and ++

                In article <slrng54jdh.asc .nospam@nospam. invalid>,
                Antoninus Twink <nospam@nospam. invalidwrote:
                >Assembly/Compiler Coding Rule 32. INC and DEC instructions should be
                >replaced with ADD or SUB instructions, because ADD and SUB overwrite all
                >flags, whereas INC and DEC do not, therefore creating false dependencies
                >on earlier instructions that set the flags.
                In some cases it can be useful to have flags preserved, but perhaps
                compilers are not yet sophisticated enough to take advantage of that
                (or rather, it's rare enough that it's not worth putting the effort
                of making them handle the added complexity).

                -- Richard
                --
                In the selection of the two characters immediately succeeding the numeral 9,
                consideration shall be given to their replacement by the graphics 10 and 11 to
                facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                Comment

                • Richard

                  #9
                  Re: About += and ++

                  Antoninus Twink <nospam@nospam. invalidwrites:
                  On 13 Jun 2008 at 10:27, santosh wrote:
                  >But the compiler may or may not emit the same instructions for i += 1
                  >versus ++i.
                  >
                  Can you name a compiler that doesn't?
                  It has become increasingly clear that Santosh merely posts "clique
                  smoothing" posts to increase his headline space. It is far easier to say
                  "possibly" than to provide any concrete answer which actually HELPS a
                  noob struggling with one single system. I am still astonished as to how
                  the regs have hijacked this group to exclude real world C questions and
                  examples.

                  Fortunately in my recent absence I see some of the regs have softened
                  their approach a little while the usual suspects have tightened up more
                  and others (read "Chuck" and "Default User") become more ridiculous with
                  every contribution they make.

                  Comment

                  • santosh

                    #10
                    Re: About += and ++

                    Richard wrote:
                    Antoninus Twink <nospam@nospam. invalidwrites:
                    >
                    >On 13 Jun 2008 at 10:27, santosh wrote:
                    >>But the compiler may or may not emit the same instructions for i +=
                    >>1 versus ++i.
                    >>
                    >Can you name a compiler that doesn't?
                    >
                    It has become increasingly clear that Santosh merely posts "clique
                    smoothing" posts to increase his headline space. It is far easier to
                    say "possibly" than to provide any concrete answer which actually
                    HELPS a noob struggling with one single system.
                    In this case the OP did not specify any particular system. To quote him:

                    "What is the compiler doing internally to increment the value in that
                    case? Or is that implementation-defined behavior?"

                    So how would you have answered? If you have a better answer, why not
                    post it?

                    <snip>

                    Comment

                    • Willem

                      #11
                      Re: About += and ++

                      santosh wrote:
                      ) In an abstract sense yes. But the compiler may or may not emit the same
                      ) instructions for i += 1 versus ++i.

                      The compiler may or may not emit the same instructions
                      for i += 1 versus i += 1,
                      this has nothing to do with the ++i versus i += 1 issue.


                      SaSW, Willem
                      --
                      Disclaimer: I am in no way responsible for any of the statements
                      made in the above text. For all I know I might be
                      drugged or something..
                      No I'm not paranoid. You all think I'm paranoid, don't you !
                      #EOT

                      Comment

                      • Richard Heathfield

                        #12
                        Re: About += and ++

                        santosh said:
                        Richard wrote:
                        >
                        <snip>
                        >>
                        >It has become increasingly clear that Santosh merely posts "clique
                        >smoothing" posts to increase his headline space. It is far easier to
                        >say "possibly" than to provide any concrete answer which actually
                        >HELPS a noob struggling with one single system.
                        >
                        In this case the OP did not specify any particular system. To quote him:
                        >
                        "What is the compiler doing internally to increment the value in that
                        case? Or is that implementation-defined behavior?"
                        >
                        So how would you have answered? If you have a better answer, why not
                        post it?
                        If he had a better answer, presumably he /would/ have posted it. We may safely
                        deduce that he doesn't have a better answer.

                        It's up to you, obviously, but you might want to reflect on the fact that he's
                        being what Dann used to call a "whinging twit". His posts (or at least the
                        ones that I see quoted by others) are almost invariably complaints about
                        other posters - which is sheer hypocrisy on his part. Do you really have the
                        time to waste on replying to him?

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • CBFalconer

                          #13
                          Re: About += and ++

                          santosh wrote:
                          Richard wrote:
                          >Antoninus Twink <nospam@nospam. invalidwrites:
                          >>santosh wrote:
                          >>>
                          >>>But the compiler may or may not emit the same instructions for
                          >>>i += 1 versus ++i.
                          >>>
                          >>Can you name a compiler that doesn't?
                          >>
                          >It has become increasingly clear that Santosh merely posts "clique
                          >smoothing" posts to increase his headline space. It is far easier
                          >to say "possibly" than to provide any concrete answer which
                          >actually HELPS a noob struggling with one single system.
                          >
                          In this case the OP did not specify any particular system. To quote
                          him:
                          >
                          "What is the compiler doing internally to increment the value in
                          that case? Or is that implementation-defined behavior?"
                          >
                          So how would you have answered? If you have a better answer, why
                          not post it?
                          Why are you responding to those two known trolls?

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.


                          ** Posted from http://www.teranews.com **

                          Comment

                          • Stephen Sprunk

                            #14
                            Re: About += and ++

                            s0suk3@gmail.co m wrote:
                            I'm new to C and having doubt about the += and ++ operators. Writing
                            'i += 1' causes the same effect on 'i' as writing '++i' (or 'i++', but
                            the fetched value will be different). But since 'i += 1' is rather
                            equivalent to 'i = i + 1', then it is a reassignment, right? So, is
                            writing '++i' or 'i++' also a reassignment? What is the compiler doing
                            internally to increment the value in that case? Or is that
                            implementation-defined behavior?
                            A major part of it is historical. In the early days of C, computer time
                            was far more expensive that programmer time, so compilers didn't
                            optimize much (if at all); even modern compilers often don't optimize by
                            default. Some constructs were known to be "faster" because the compiler
                            could easily use a simpler instruction for "++i" than for "i+=1" or
                            "i=i+1". Each syntax translated directly into machine instructions, so
                            the programmer could take advantage of those "easy" optimizations when
                            they applied.

                            Today, of course, all of those constructs will yield the exact same code
                            if they have the same meaning when a modern optimizing compiler is used.

                            Another major part of it is laziness. "++i" obviously takes less typing
                            than "i=i+1", and takes less space to store in source form as well.
                            When you get to more complicated idioms, like "while (*i++ = *j++);" vs
                            "do { *i=*j; i=i+1; j=j+1; } while (*j);", the space savings gets
                            significant, as does readability. Such things appear over and over in
                            most programs, so it's normal that a language that "evolved" (as opposed
                            to one that was "designed") would incorporate features to make the
                            programmer's life easier for common tasks.

                            S

                            Comment

                            • Ben Bacarisse

                              #15
                              Re: About += and ++

                              Stephen Sprunk <stephen@sprunk .orgwrites:
                              >... When you get to more complicated idioms, like "while (*i++ =
                              *j++);" vs "do { *i=*j; i=i+1; j=j+1; } while (*j);", the space
                              savings gets significant, as does readability.
                              Note that they do different things. The "vs" suggests that they are
                              alternatives.

                              --
                              Ben.

                              Comment

                              Working...