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. type size

type size

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 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.
  • K Offline
    K Offline
    Kamis
    wrote on last edited by
    #1

    Well,I look into limits.h and see that "short" consists of 2 bytes on my machine and there are 4 bytes for "int" type. So, 1) what does it depend on? (OS, compiler, CPU) 2) Suppose I use such a variable in my prog: short a; a=32000; and than run it on the machine where "short" consists of 1 byte. Will it work correctly?

    B G H 3 Replies Last reply
    0
    • K Kamis

      Well,I look into limits.h and see that "short" consists of 2 bytes on my machine and there are 4 bytes for "int" type. So, 1) what does it depend on? (OS, compiler, CPU) 2) Suppose I use such a variable in my prog: short a; a=32000; and than run it on the machine where "short" consists of 1 byte. Will it work correctly?

      B Offline
      B Offline
      bneacetp
      wrote on last edited by
      #2
      1. From my understanding, the size of the variable depends on the compiler and/or OS (whether 16bit-, 32bit-, or 64bit-based I think). 2) If you ever ran into a situation where a short took only a byte, you wouldn't be able to get a to 32000. The highest number you could get to would be 255. Look below at the binary math: 1111 1111 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 I doubt though that you will ever run into a situation where a short will only take a byte of memory. I hope that this helps. Happy Programming and God Bless! "Your coding practices might be buggy, but your code is always right." Internet::WWW::CodeProject::bneacetp
      A 1 Reply Last reply
      0
      • B bneacetp
        1. From my understanding, the size of the variable depends on the compiler and/or OS (whether 16bit-, 32bit-, or 64bit-based I think). 2) If you ever ran into a situation where a short took only a byte, you wouldn't be able to get a to 32000. The highest number you could get to would be 255. Look below at the binary math: 1111 1111 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 I doubt though that you will ever run into a situation where a short will only take a byte of memory. I hope that this helps. Happy Programming and God Bless! "Your coding practices might be buggy, but your code is always right." Internet::WWW::CodeProject::bneacetp
        A Offline
        A Offline
        Antony M Kancidrowski
        wrote on last edited by
        #3

        NO! The maximum number you can get in 8 bits is 255 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 !!! Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fuity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        N 1 Reply Last reply
        0
        • K Kamis

          Well,I look into limits.h and see that "short" consists of 2 bytes on my machine and there are 4 bytes for "int" type. So, 1) what does it depend on? (OS, compiler, CPU) 2) Suppose I use such a variable in my prog: short a; a=32000; and than run it on the machine where "short" consists of 1 byte. Will it work correctly?

          G Offline
          G Offline
          Graham Bradshaw
          wrote on last edited by
          #4

          Kamis wrote: what does it depend on The ANSI standards do not impose any restrictions on the size of short, int or long data types, and for portable code, you should make no assumptions. Famously, some (many) years ago, the Microsoft C++ compiler changed the size of an int from 2 bytes to 4 bytes, and broke all sorts of code (including a lot of internal MS stuff), that had hard-coded the assumption that sizeof(int) was 2. In the Windows world, if you need a guaranteed size, you can use BYTE, WORD and DWORD data types. If you need code to be portable, you have to make use of sizeof() and all of the #defines in limits.h and/or other header files.

          K 1 Reply Last reply
          0
          • G Graham Bradshaw

            Kamis wrote: what does it depend on The ANSI standards do not impose any restrictions on the size of short, int or long data types, and for portable code, you should make no assumptions. Famously, some (many) years ago, the Microsoft C++ compiler changed the size of an int from 2 bytes to 4 bytes, and broke all sorts of code (including a lot of internal MS stuff), that had hard-coded the assumption that sizeof(int) was 2. In the Windows world, if you need a guaranteed size, you can use BYTE, WORD and DWORD data types. If you need code to be portable, you have to make use of sizeof() and all of the #defines in limits.h and/or other header files.

            K Offline
            K Offline
            Kamis
            wrote on last edited by
            #5

            Thanks for the help!

            1 Reply Last reply
            0
            • A Antony M Kancidrowski

              NO! The maximum number you can get in 8 bits is 255 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 !!! Ant. I'm hard, yet soft.
              I'm coloured, yet clear.
              I'm fuity and sweet.
              I'm jelly, what am I? Muse on it further, I shall return!
              - David Williams (Little Britain)

              N Offline
              N Offline
              normanS
              wrote on last edited by
              #6

              Being pedantic, INT is a signed value. For an 8-bit value, the range is 0000 0000 to 1111 1111. BUT . . . 0000 0001 to 0111 1111 are positive, while for 1000 0000 to 1111 1111, the sign bit (MSb) is 1, so these are negative numbers. The range is thus -128 to +127 for a 1-byte INT. 10 or 15 years ago, I did some development on Windows 3.11 using Turbo C++ 3.1 (16-bit Integers.) I carefully used only ANSI C commands, for portability, but I also used INTs, instead of SHORT & LONG. This really caused problems when I ported the code to a MicroVax running VMS (32-bit INT, as I recall.)

              A 1 Reply Last reply
              0
              • K Kamis

                Well,I look into limits.h and see that "short" consists of 2 bytes on my machine and there are 4 bytes for "int" type. So, 1) what does it depend on? (OS, compiler, CPU) 2) Suppose I use such a variable in my prog: short a; a=32000; and than run it on the machine where "short" consists of 1 byte. Will it work correctly?

                H Offline
                H Offline
                Henry miller
                wrote on last edited by
                #7

                Once upon a time, in a land not so very far from here there was a machine. This machine seems just like any other computer at first glance, but when you look closer you discover the size of short, int, and long are all 36 bits! Don't make any assumptions lest you one day be called upon to port you code to that machine. You can of course decide how far to go. If your program is using MFC you can be confident that porting to non-windows won't happen, so you only need to consider windows. Microsoft is unlikely to make short less than 16 bits, but it might be bigger. In general it is safe to assume that short is 16 bits because any machine where that is not the case will either have more bits, or other serious limitations that you have to take into account from the start of design. (they still make 4 bit CPUs, but the code that runs on them is all written in house) More important these days is to note that sizeof(int) is no longer == sizeof(VOID *). This is breaking a lot of code, 64 bit CPUs are here today some people even have them on their desktop without knowing it. (though windows doesn't use all 64 bits yet, that will come)

                1 Reply Last reply
                0
                • N normanS

                  Being pedantic, INT is a signed value. For an 8-bit value, the range is 0000 0000 to 1111 1111. BUT . . . 0000 0001 to 0111 1111 are positive, while for 1000 0000 to 1111 1111, the sign bit (MSb) is 1, so these are negative numbers. The range is thus -128 to +127 for a 1-byte INT. 10 or 15 years ago, I did some development on Windows 3.11 using Turbo C++ 3.1 (16-bit Integers.) I carefully used only ANSI C commands, for portability, but I also used INTs, instead of SHORT & LONG. This really caused problems when I ported the code to a MicroVax running VMS (32-bit INT, as I recall.)

                  A Offline
                  A Offline
                  Antony M Kancidrowski
                  wrote on last edited by
                  #8

                  NormanS wrote: Being pedantic, INT is a signed value. Quite, though I was pointing out the mistake in binary mathematics. Still my statement stands as I do not metion type ;) Ant. I'm hard, yet soft.
                  I'm coloured, yet clear.
                  I'm fuity and sweet.
                  I'm jelly, what am I? Muse on it further, I shall return!
                  - David Williams (Little Britain)

                  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