Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. beginner, simple c question.

beginner, simple c question.

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antionette
    wrote on last edited by
    #1

    int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.

    L L CPalliniC C 4 Replies Last reply
    0
    • A antionette

      int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What has this to do with C? This is simple mathematics, and if you find this hard to understand, I doubt that you will find programming too easy. I would suggest you get a copy of a good C or C++ book and read it thoroughly.

      MVP 2010 - are they mad?

      C 1 Reply Last reply
      0
      • A antionette

        int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.

        L Offline
        L Offline
        loyal ginger
        wrote on last edited by
        #3

        I guess this should be correct: i + j = -20 + 10 = -10

        D 1 Reply Last reply
        0
        • A antionette

          int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          antionette wrote:

          int i=-20; unsigned j=10;

          Fine.

          antionette wrote:

          i+j=?

          The above doesn't compile.

          antionette wrote:

          And why?

          Because it is not a valid C statement.

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • A antionette

            int i=-20; unsigned j=10; i+j=? And why? I am a beginner to C. Thanks for reply first.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            antionette wrote:

            i+j=?

            that's actually a tricky question. there are a lot of (really strange and non-intuitive, IMO) rules about how mixed signed/unsigned math is done in C. and a lot of those rules depend on how your compiler interprets the standard, and which standard. i've found that whenever i'm doing signed/unsigned arithmetic, it's best to cast everything to a signed value, then perform the arithmetic. if the result needs to be unsigned, then i range-check the value to make sure it's non-negative and small enough to fit the result. in this case, the answer is a huge positive integer.

            int i = -20;
            unsigned  j = 10;
            \_\_int64 k = i + j;
            

            k = 4294967286

            image processing toolkits | batch image processing

            modified on Friday, January 15, 2010 11:22 AM

            L 1 Reply Last reply
            0
            • L Lost User

              What has this to do with C? This is simple mathematics, and if you find this hard to understand, I doubt that you will find programming too easy. I would suggest you get a copy of a good C or C++ book and read it thoroughly.

              MVP 2010 - are they mad?

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              Richard MacCutchan wrote:

              This is simple mathematics

              it really isn't. try it in a compiler.

              image processing toolkits | batch image processing

              L 1 Reply Last reply
              0
              • L loyal ginger

                I guess this should be correct: i + j = -20 + 10 = -10

                D Offline
                D Offline
                dxlee
                wrote on last edited by
                #7

                I noticed the "Joke" icon. I give you a 5. It is tricky so I think the original post is a pretty good beginner's question. My daughter asks "why" a lot. It's a learning process.

                1 Reply Last reply
                0
                • C Chris Losinger

                  Richard MacCutchan wrote:

                  This is simple mathematics

                  it really isn't. try it in a compiler.

                  image processing toolkits | batch image processing

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Chris Losinger wrote:

                  try it in a compiler.

                  int i = -20;
                  unsigned int j = 10;
                  int k = i + j;
                  

                  answer is -10. However if I just print the value (i + j) it returns 4294967286 obviously an unsigned integer, so some interesting automatic casting going on. I stand corrected! [edit]the answer is -10 of course![/edit]

                  MVP 2010 - are they mad?

                  modified on Friday, January 15, 2010 12:50 PM

                  1 Reply Last reply
                  0
                  • C Chris Losinger

                    antionette wrote:

                    i+j=?

                    that's actually a tricky question. there are a lot of (really strange and non-intuitive, IMO) rules about how mixed signed/unsigned math is done in C. and a lot of those rules depend on how your compiler interprets the standard, and which standard. i've found that whenever i'm doing signed/unsigned arithmetic, it's best to cast everything to a signed value, then perform the arithmetic. if the result needs to be unsigned, then i range-check the value to make sure it's non-negative and small enough to fit the result. in this case, the answer is a huge positive integer.

                    int i = -20;
                    unsigned  j = 10;
                    \_\_int64 k = i + j;
                    

                    k = 4294967286

                    image processing toolkits | batch image processing

                    modified on Friday, January 15, 2010 11:22 AM

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Good info - I think the general rule is: don't use unsigned numbers in calculations.

                    MVP 2010 - are they mad?

                    CPalliniC 1 Reply Last reply
                    0
                    • L Lost User

                      Good info - I think the general rule is: don't use unsigned numbers in calculations.

                      MVP 2010 - are they mad?

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Richard MacCutchan wrote:

                      Good info - I think the general rule is: don't use unsigned numbers in calculations.

                      Don't mix unsigned and signed numbers, without proper explicit casts, in calculations. FFY. :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups