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. unsigned and signed in C

unsigned and signed in C

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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
    Anonygeeker
    wrote on last edited by
    #1

    hi, The default integer declaration assumes a signed number. But when i tried unsigned int a=-26; printf("\n%d",a); it printed "-26" only. Why?

    S 1 Reply Last reply
    0
    • A Anonygeeker

      hi, The default integer declaration assumes a signed number. But when i tried unsigned int a=-26; printf("\n%d",a); it printed "-26" only. Why?

      S Offline
      S Offline
      Shaurya Sinha
      wrote on last edited by
      #2

      Hi,

      It is undefined behaviour because you have used wrong format specifier. Use %u instead of %d for unsigned int. like :

      unsigned int a=-26;
      printf("\n%u",a);

      A R 2 Replies Last reply
      0
      • S Shaurya Sinha

        Hi,

        It is undefined behaviour because you have used wrong format specifier. Use %u instead of %d for unsigned int. like :

        unsigned int a=-26;
        printf("\n%u",a);

        A Offline
        A Offline
        Anonygeeker
        wrote on last edited by
        #3

        Thanks. Is it only about the format specifier? Or some other conversion happens inside?

        S 1 Reply Last reply
        0
        • A Anonygeeker

          Thanks. Is it only about the format specifier? Or some other conversion happens inside?

          S Offline
          S Offline
          Shaurya Sinha
          wrote on last edited by
          #4

          Yes correct. If a conversion specification is invalid, the behavior is undefined.

          If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

          1 Reply Last reply
          0
          • S Shaurya Sinha

            Hi,

            It is undefined behaviour because you have used wrong format specifier. Use %u instead of %d for unsigned int. like :

            unsigned int a=-26;
            printf("\n%u",a);

            R Offline
            R Offline
            Rick York
            wrote on last edited by
            #5

            No, it is most definitely NOT undefined behavior. This is exactly the behavior one should expect. The variable a has the binary value of -26 decimal, or 0xFFFFFFE6 in 32-bit hexadecimal. When it is passed to printf with a %d format specifier it will be interpreted as a signed decimal value and -26 is printed. There is nothing undefined about that. It is interpreting the variable exactly as the format specifier is defined to. It may not be what you want but it is as expected.

            D 1 Reply Last reply
            0
            • R Rick York

              No, it is most definitely NOT undefined behavior. This is exactly the behavior one should expect. The variable a has the binary value of -26 decimal, or 0xFFFFFFE6 in 32-bit hexadecimal. When it is passed to printf with a %d format specifier it will be interpreted as a signed decimal value and -26 is printed. There is nothing undefined about that. It is interpreting the variable exactly as the format specifier is defined to. It may not be what you want but it is as expected.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Rick York wrote:

              It may not be what you want but it is as expected.

              :thumbsup:

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              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