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. So, how good are you? ;) (C/C++)

So, how good are you? ;) (C/C++)

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    Shankar Chandra Bose
    wrote on last edited by
    #1

    Here's a test for all you C/C++ programmers: anything strange in the following code fragment? (Try not to compile it, okay? Just see if you can spot anything strange!) char ch; for (ch = 0;ch <= 255;ch++) std::cout << ch;

    L 2 Replies Last reply
    0
    • S Shankar Chandra Bose

      Here's a test for all you C/C++ programmers: anything strange in the following code fragment? (Try not to compile it, okay? Just see if you can spot anything strange!) char ch; for (ch = 0;ch <= 255;ch++) std::cout << ch;

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

      There is no opening and closing brackets for the cout instruction?:confused: This works with the microsoft compiler but not with C++ Builder. (Am I wrong? - I really didn't cheat):-D

      S 1 Reply Last reply
      0
      • S Shankar Chandra Bose

        Here's a test for all you C/C++ programmers: anything strange in the following code fragment? (Try not to compile it, okay? Just see if you can spot anything strange!) char ch; for (ch = 0;ch <= 255;ch++) std::cout << ch;

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

        Besides the first 31 characters containing control and unprintable characters, I would say the problem would be that 255 would never be reached. char can contain values -128 through to 127. As soon as the eighth bit was filled it would become a negative number. So how did I go? Am I as good as I tell my wife I am or what !!! Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

        L S 2 Replies Last reply
        0
        • L Lost User

          Besides the first 31 characters containing control and unprintable characters, I would say the problem would be that 255 would never be reached. char can contain values -128 through to 127. As soon as the eighth bit was filled it would become a negative number. So how did I go? Am I as good as I tell my wife I am or what !!! Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

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

          Even with unsigned char, you would have trouble because the loop condition is for i <= 255, so even an unsigned char would roll over to 0 without terminating the loop.

          L 1 Reply Last reply
          0
          • L Lost User

            There is no opening and closing brackets for the cout instruction?:confused: This works with the microsoft compiler but not with C++ Builder. (Am I wrong? - I really didn't cheat):-D

            S Offline
            S Offline
            Shankar Chandra Bose
            wrote on last edited by
            #5

            oops, wrong answer, Frederick! See the other posts!

            1 Reply Last reply
            0
            • L Lost User

              Besides the first 31 characters containing control and unprintable characters, I would say the problem would be that 255 would never be reached. char can contain values -128 through to 127. As soon as the eighth bit was filled it would become a negative number. So how did I go? Am I as good as I tell my wife I am or what !!! Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

              S Offline
              S Offline
              Shankar Chandra Bose
              wrote on last edited by
              #6

              Bingo! I'd say your wife is lucky. On the other hand, you could be a nerd...:)

              1 Reply Last reply
              0
              • L Lost User

                Even with unsigned char, you would have trouble because the loop condition is for i <= 255, so even an unsigned char would roll over to 0 without terminating the loop.

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

                Not true. unsigned char can hold a value of 255. Formula is 2n -1 (where n is the number of bits in the data type)for unsigned values. In this case it is 28 - 1 = 255. Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

                M 1 Reply Last reply
                0
                • L Lost User

                  Not true. unsigned char can hold a value of 255. Formula is 2n -1 (where n is the number of bits in the data type)for unsigned values. In this case it is 28 - 1 = 255. Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

                  M Offline
                  M Offline
                  Mikhail Ermishkin
                  wrote on last edited by
                  #8

                  Of course unsigned char can hold 255, but to terminate this loop you need 256, look again on condition: ch<=255; Do you feel that you still good? :)

                  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