data types

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

    #1

    data types

    Hi,

    I've been working on this project for the past few months in my spare
    time, and now I started working on the windows interface and going
    over my petzold book, I've come to the realization that an int could
    be 32-bit for PCs. Oh, I could kick myself for not checking good in
    the beginning, but the manual for the compiler I am using says int is
    16-bit. It may be out of date.

    Anyway, now that I need to go back over and look closely at my code,
    my question is: is there a way to declare a variable say a 16 bit
    unsigned integer in C? Or is declaring it "short" the only specifier
    that may work?

    Thanks in advance

  • Roberto Waltman

    #2
    Re: data types

    koolj96825@yaho o.com wrote:
    >... an int could
    >be 32-bit for PCs.
    >...but the manual for the compiler I am using says int is
    >16-bit.
    >
    >Anyway, now that I need to go back over and look closely at my code,
    >my question is: is there a way to declare a variable say a 16 bit
    >unsigned integer in C? Or is declaring it "short" the only specifier
    >that may work?
    For non-C99 compilers, the common, non-portable way of doing it is:

    typedef [whatever type is 16 bits in your platform] int16;
    typedef [whatever type is 32 bits in your platform] int32;

    and so on. Then use int16/32/etc. instead of plain int in your code.


    >Thanks in advance
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]

    Comment

    • Ian Collins

      #3
      Re: data types

      Roberto Waltman wrote:
      koolj96825@yaho o.com wrote:
      >
      >
      >>... an int could
      >>be 32-bit for PCs.
      >>...but the manual for the compiler I am using says int is
      >>16-bit.
      >>
      >>Anyway, now that I need to go back over and look closely at my code,
      >>my question is: is there a way to declare a variable say a 16 bit
      >>unsigned integer in C? Or is declaring it "short" the only specifier
      >>that may work?
      >
      For non-C99 compilers, the common, non-portable way of doing it is:
      >
      typedef [whatever type is 16 bits in your platform] int16;
      typedef [whatever type is 32 bits in your platform] int32;
      >
      and so on. Then use int16/32/etc. instead of plain int in your code.
      >
      int16_t would be better, to match the types in <stdint.h>.

      --
      Ian Collins.

      Comment

      • santosh

        #4
        Re: data types

        koolj96825@yaho o.com wrote:
        Hi,
        >
        I've been working on this project for the past few months in my spare
        time, and now I started working on the windows interface and going
        over my petzold book, I've come to the realization that an int could
        be 32-bit for PCs.
        For a particular C implementation an int is (sizeof(int) * CHAR_BIT)
        bits. For modern PCs it's often 32 or 64.
        Oh, I could kick myself for not checking good in
        the beginning, but the manual for the compiler I am using says int is
        16-bit. It may be out of date.
        Could be.
        Anyway, now that I need to go back over and look closely at my code,
        my question is: is there a way to declare a variable say a 16 bit
        unsigned integer in C? Or is declaring it "short" the only specifier
        that may work?
        Why do you need an object of exactly N bits?

        If your compiler is not a C99 one, as it appears to be, then you'll
        have to define a 16-bit type yourself. It's not difficult. For
        example, if, under your implementation, int happens be 16 bits then
        unsigned int is the type you want. You can create an alias like:

        typedef unsigned int int16;

        But unless you have a specific requirement, I suggest installing a
        current compiler system. You have many choices for Windows, though I
        recommend either a version of gcc, (Cygwin or MinGW), or Visual Studio
        Express.

        If your implementation does support C99, (and you don't mind partial
        support), have a look at the types in stdint.h. uint16_t or
        uint_least16_t may meet your needs.

        Comment

        • CBFalconer

          #5
          Re: data types

          Roberto Waltman wrote:
          koolj96825@yaho o.com wrote:
          >
          >... an int could be 32-bit for PCs.
          >...but the manual for the compiler I am using says int is 16-bit.
          >>
          >Anyway, now that I need to go back over and look closely at my
          >code, my question is: is there a way to declare a variable say a
          >16 bit unsigned integer in C? Or is declaring it "short" the only
          >specifier that may work?
          >
          For non-C99 compilers, the common, non-portable way of doing it is:
          >
          typedef [whatever type is 16 bits in your platform] int16;
          typedef [whatever type is 32 bits in your platform] int32;
          >
          and so on. Then use int16/32/etc. instead of plain int in your code.
          Better to look at the range of values required and set your
          fundamental types accordingly. Just don't bother with such
          constraints as the above, as there may be no such type extant.

          --
          Chuck F (cbfalconer at maineline dot net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net>


          Comment

          • Rahul

            #6
            Re: data types

            Hi Guys,
            i just want to extend the topic a little further.....
            what governs the size of a data type? is it the computer architecture
            or the compiler which is generating the executable code for a
            particular architecture?
            Also, does the compiler take care of the endianness of the machine?
            What is the case with a cross compiler in such situations?

            thanx.
            rk.
            koolj96825@yaho o.com wrote:
            Hi,
            >
            I've been working on this project for the past few months in my spare
            time, and now I started working on the windows interface and going
            over my petzold book, I've come to the realization that an int could
            be 32-bit for PCs. Oh, I could kick myself for not checking good in
            the beginning, but the manual for the compiler I am using says int is
            16-bit. It may be out of date.
            >
            Anyway, now that I need to go back over and look closely at my code,
            my question is: is there a way to declare a variable say a 16 bit
            unsigned integer in C? Or is declaring it "short" the only specifier
            that may work?
            >
            Thanks in advance

            Comment

            • koolj96825@yahoo.com

              #7
              Re: data types

              16-bit. It may be out of date.
              >
              Could be.
              Yes, I think the manual was not kept up.
              sizeof tells me there are 4 bytes to an int.
              >
              Anyway, now that I need to go back over and look closely at my code,
              my question is: is there a way to declare a variable say a 16 bit
              unsigned integer in C? Or is declaring it "short" the only specifier
              that may work?
              >
              Why do you need an object of exactly N bits?
              I have at least two issues where it matters. In one case, I created a
              data type which is similar in concept to a bcd (binary coded decimal)
              for working with degrees-minutes-seconds. I am wasting too much
              memory per dms structure if they are 32-bit integers. I'll change it
              to chars.

              Next, I was using them as keys and since some quirk in the compiler
              didn't let me use the constant for a max unsigned int, I made my own
              constant. I also expected the number to "wrap around" at that value.
              Another issue is that I used a bit vector to map out used numbers, as
              a 16-bit key, an acceptably small amount of memory would be used up,
              but a 32-bit value may be overboard especially since I don't expect
              more than a few hundred keys to be in my data structure.

              >
              If your compiler is not a C99 one, as it appears to be, then you'll
              have to define a 16-bit type yourself. It's not difficult. For
              example, if, under your implementation, int happens be 16 bits then
              unsigned int is the type you want. You can create an alias like:
              >
              typedef unsigned int int16;
              I have now implemented more typedefs for this.

              >
              But unless you have a specific requirement, I suggest installing a
              current compiler system. You have many choices for Windows, though I
              recommend either a version of gcc, (Cygwin or MinGW), or Visual Studio
              Express.
              >

              I only program on the hobby level, although I am doing this project
              for my office, I thus have no budget. So, I am using one of the free
              compilers. I would love to upgrade but since I do this on the hobby
              level, I don't know one compiler from another. I will investigate
              your suggestions.

              If your implementation does support C99, (and you don't mind partial
              support), have a look at the types in stdint.h. uint16_t or
              uint_least16_t may meet your needs.

              I will need to google this. Thank you.

              Comment

              • santosh

                #8
                Re: data types

                koolj96825@yaho o.com wrote:
                16-bit. It may be out of date.
                Could be.
                >
                Yes, I think the manual was not kept up.
                sizeof tells me there are 4 bytes to an int.
                Then it's most probably a 32 bit object. What does sizeof(short)
                indicate? Usually short is smaller than an int.
                Anyway, now that I need to go back over and look closely at my code,
                my question is: is there a way to declare a variable say a 16 bit
                unsigned integer in C? Or is declaring it "short" the only specifier
                that may work?
                Why do you need an object of exactly N bits?
                >
                I have at least two issues where it matters. In one case, I created a
                data type which is similar in concept to a bcd (binary coded decimal)
                for working with degrees-minutes-seconds. I am wasting too much
                memory per dms structure if they are 32-bit integers. I'll change it
                to chars.
                Are you trying to pack multiple values into an object? Is the runtime
                environment really resource constrained? Unless you use thousands of
                such objects, it may not be a significant saving, though of course I'm
                speaking from ignorance of your system's limitations.
                Next, I was using them as keys and since some quirk in the compiler
                didn't let me use the constant for a max unsigned int, I made my own
                constant.
                Your compiler is either *really* old or broken or both. I strongly
                suggest replacing it.
                I also expected the number to "wrap around" at that value.
                If it is equal to (2^n) - 1, where n is the number of value bits in
                your object, then it should.
                Another issue is that I used a bit vector to map out used numbers, as
                a 16-bit key, an acceptably small amount of memory would be used up,
                but a 32-bit value may be overboard especially since I don't expect
                more than a few hundred keys to be in my data structure.
                Have you considered a bitfield as an alternative?

                <snip>
                But unless you have a specific requirement, I suggest installing a
                current compiler system. You have many choices for Windows, though I
                recommend either a version of gcc, (Cygwin or MinGW), or Visual Studio
                Express.
                >
                I only program on the hobby level, although I am doing this project
                for my office, I thus have no budget.
                Both the compilers I mentioned are free to download and use, (though
                Microsoft's Visual Studio Express has an onerous EULA). gcc is
                absolutely free, available for just about every platform, has very
                decent C99 support and is totally free to use. It's a world class
                compiler. Most regulars here will recommend gcc to you.

                Beware though that gcc by itself doesn't include the standard C
                library. However your operating system should include a C library,
                which gcc can use.

                Comment

                • santosh

                  #9
                  Re: data types


                  Rahul wrote:

                  Please don't top-post.
                  [fixed]
                  koolj96825@yaho o.com wrote:
                  Hi,

                  I've been working on this project for the past few months in my spare
                  time, and now I started working on the windows interface and going
                  over my petzold book, I've come to the realization that an int could
                  be 32-bit for PCs. Oh, I could kick myself for not checking good in
                  the beginning, but the manual for the compiler I am using says int is
                  16-bit. It may be out of date.

                  Anyway, now that I need to go back over and look closely at my code,
                  my question is: is there a way to declare a variable say a 16 bit
                  unsigned integer in C? Or is declaring it "short" the only specifier
                  that may work?

                  Thanks in advance
                  >
                  Hi Guys,
                  i just want to extend the topic a little further.....
                  what governs the size of a data type? is it the computer architecture
                  or the compiler which is generating the executable code for a
                  particular architecture?
                  Usually a system's compiler makes choices based on the underlying
                  architecture of the system. Most systems have a natural word size,
                  (which is usually the width of the processor's general purpose
                  registers), they also have a byte as the smallest independently
                  addressed data unit. They may also provide special data objects, (like
                  floating point and SIMD registers), larger than the general purpose
                  registers etc.

                  The compiler makes it's choices based on the target system. Usually
                  the int type corresponds to the machine's word, char is implemented on
                  a byte and others like float and long long may be implemented with the
                  system's floating point unit and other special registers.
                  Also, does the compiler take care of the endianness of the machine?
                  Endianess isn't much of an issue since the compiled machine code is
                  specific only to that machine type. It does become an issue when data
                  is exchanged between systems, but that's not within the purview of the
                  compiler. Usually system routines take care of the translation.
                  What is the case with a cross compiler in such situations?
                  The cross compiler emits object code for it's target, with it's
                  endianess accounted for. The host system doesn't matter, other than
                  hosting the compiler itself.

                  Comment

                  • Malcolm McLean

                    #10
                    Re: data types


                    <koolj96825@yah oo.comwrote in message
                    I've been working on this project for the past few months in my spare
                    time, and now I started working on the windows interface and going
                    over my petzold book, I've come to the realization that an int could
                    be 32-bit for PCs. Oh, I could kick myself for not checking good in
                    the beginning, but the manual for the compiler I am using says int is
                    16-bit. It may be out of date.
                    >
                    Anyway, now that I need to go back over and look closely at my code,
                    my question is: is there a way to declare a variable say a 16 bit
                    unsigned integer in C? Or is declaring it "short" the only specifier
                    that may work?
                    >
                    What do you wnat a 16 bit unsigned integer for?

                    Normally you want something to hold an integer that is big enough to never
                    overflow. For instance an image is probably never going to be more than
                    about 10,000 pixels in either dimension. However whether you have 16 or 32
                    bits to hold the widtha nd height should be a matter of indifference to you.

                    Occasionally you do need to interface C to other languages, and these things
                    become important. However not as a rule.
                    --
                    Free games and programming goodies.


                    Comment

                    • koolj96825@yahoo.com

                      #11
                      Re: data types

                      Then it's most probably a 32 bit object. What does sizeof(short)
                      indicate? Usually short is smaller than an int.
                      short is 2 bytes.

                      I guess I'll be using that. Originally I thought if it came out good,
                      I'd port myself a version to ubuntu, but now I don't really care, so
                      portability issues are less important now. I thought it'd be easier
                      to be portable in C.
                      >
                      Anyway, now that I need to go back over and look closely at my code,
                      my question is: is there a way to declare a variable say a 16 bit
                      unsigned integer in C? Or is declaring it "short" the only specifier
                      that may work?
                      >
                      Why do you need an object of exactly N bits?
                      I have at least two issues where it matters. In one case, I created a
                      data type which is similar in concept to a bcd (binary coded decimal)
                      for working with degrees-minutes-seconds. I am wasting too much
                      memory per dms structure if they are 32-bit integers. I'll change it
                      to chars.
                      >
                      Are you trying to pack multiple values into an object? Is the runtime
                      environment really resource constrained? Unless you use thousands of
                      such objects, it may not be a significant saving, though of course I'm
                      speaking from ignorance of your system's limitations.
                      >
                      You know, you are absolutely right! I guess I am just soooo used to
                      programming HP-48GXs in system RPL with 512K mem. I get a little
                      worried about wasting memory when I don't need to.

                      Next, I was using them as keys and since some quirk in the compiler
                      didn't let me use the constant for a max unsigned int, I made my own
                      constant.
                      >
                      Yes, I wondered why MAX_UINT or whatever it was called would work in
                      one part of my program, and when I used it in another place, I'd get
                      compiler errors. I guess that's why there were higher versions of it.

                      Your compiler is either *really* old or broken or both. I strongly
                      suggest replacing it.
                      >
                      I also expected the number to "wrap around" at that value.
                      >
                      If it is equal to (2^n) - 1, where n is the number of value bits in
                      your object, then it should.
                      >
                      Another issue is that I used a bit vector to map out used numbers, as
                      a 16-bit key, an acceptably small amount of memory would be used up,
                      but a 32-bit value may be overboard especially since I don't expect
                      more than a few hundred keys to be in my data structure.
                      >
                      Have you considered a bitfield as an alternative?
                      >
                      Bit field? That may have been the proper term. I may have misspoken.
                      Sorry.
                      <snip>
                      >
                      But unless you have a specific requirement, I suggest installing a
                      current compiler system. You have many choices for Windows, though I
                      recommend either a version of gcc, (Cygwin or MinGW), or Visual Studio
                      Express.
                      >
                      I only program on the hobby level, although I am doing this project
                      for my office, I thus have no budget.
                      >
                      Both the compilers I mentioned are free to download and use, (though
                      Microsoft's Visual Studio Express has an onerous EULA). gcc is
                      absolutely free, available for just about every platform, has very
                      decent C99 support and is totally free to use. It's a world class
                      compiler. Most regulars here will recommend gcc to you.
                      >
                      Beware though that gcc by itself doesn't include the standard C
                      library. However your operating system should include a C library,
                      which gcc can use.
                      Would changing compilers cause unexpected problems? (logic or syntax
                      or something like that?)

                      Comment

                      • Flash Gordon

                        #12
                        Re: data types

                        koolj96825@yaho o.com wrote, On 03/03/07 07:00:
                        >>16-bit. It may be out of date.
                        >Could be.
                        >
                        Yes, I think the manual was not kept up.
                        sizeof tells me there are 4 bytes to an int.
                        Just remember, next year it might be 8, or you might get in to embedded
                        work and come across systems with 16 bit chars (you might not want to
                        worry about this, I don't for what I'm currently doing).
                        >>Anyway, now that I need to go back over and look closely at my code,
                        >>my question is: is there a way to declare a variable say a 16 bit
                        >>unsigned integer in C? Or is declaring it "short" the only specifier
                        >>that may work?
                        >Why do you need an object of exactly N bits?
                        >
                        I have at least two issues where it matters. In one case, I created a
                        data type which is similar in concept to a bcd (binary coded decimal)
                        for working with degrees-minutes-seconds. I am wasting too much
                        memory per dms structure if they are 32-bit integers. I'll change it
                        to chars.
                        Depending on what you are doing, you might want to consider (if you are
                        not already using) as an alternative what is generally used where I used
                        to work and generally called with "wrap angles" or BAMs (Binary Angular
                        Measurement, I think). Basically, it was a 16 bit scaled integer where
                        90 degrees was represented by 0x4000. We generally assumed 2s complement
                        signed numbers, but with hindsight the code would have worked
                        identically if the variables were declared as unsigned 16 bit integers
                        (this was before I found this group and learnt a lot more about C).
                        Unsigned integers are guaranteed to wrap as you expect.
                        Next, I was using them as keys and since some quirk in the compiler
                        didn't let me use the constant for a max unsigned int, I made my own
                        constant.
                        If UINT_MAX is not declared (or declared incorrectly) through away the
                        implementation and use something else. First make sure that the error
                        was not yours!
                        I also expected the number to "wrap around" at that value.
                        Unsigned integer types are guaranteed by the C standard to wrap.
                        Another issue is that I used a bit vector to map out used numbers, as
                        a 16-bit key, an acceptably small amount of memory would be used up,
                        but a 32-bit value may be overboard especially since I don't expect
                        more than a few hundred keys to be in my data structure.
                        Use "unsigned short" if space is a concern and 16 bits is definitely
                        large enough. It will be 16 bits on all the systems you are likely to
                        come across until 128 bit processors come out and desktops by default
                        come with 16GB of RAM.
                        >If your compiler is not a C99 one, as it appears to be, then you'll
                        >have to define a 16-bit type yourself. It's not difficult. For
                        >example, if, under your implementation, int happens be 16 bits then
                        >unsigned int is the type you want. You can create an alias like:
                        >>
                        > typedef unsigned int int16;
                        >
                        I have now implemented more typedefs for this.
                        Note that short is almost certainly going to be 16 bits on machines with
                        either 16 or 32 bit int, so using short in your typedef will mean having
                        to change it less often.
                        >But unless you have a specific requirement, I suggest installing a
                        >current compiler system. You have many choices for Windows, though I
                        >recommend either a version of gcc, (Cygwin or MinGW), or Visual Studio
                        >Express.
                        >
                        I only program on the hobby level, although I am doing this project
                        for my office, I thus have no budget. So, I am using one of the free
                        compilers. I would love to upgrade but since I do this on the hobby
                        level, I don't know one compiler from another. I will investigate
                        your suggestions.
                        They are good suggestions. There are also various IDEs using gcc. I
                        suggest you look at http://clc-wiki.net/wiki/C_resources:Compilers and


                        Note that mention on one of those pages is not an endorsement, nor is
                        being left off a sign it is bad.

                        Personally I am using gcc for the commercial SW I work on.
                        >If your implementation does support C99, (and you don't mind partial
                        >support), have a look at the types in stdint.h. uint16_t or
                        >uint_least16 _t may meet your needs.
                        >
                        I will need to google this. Thank you.
                        If you search the archives of this group you will find references to a
                        fairly portable implementation of stdint.h for implementations that do
                        not provide it.

                        If would be useful if C had "#ifincexis ts" to check if an include file
                        is available, but it does not.
                        --
                        Flash Gordon

                        Comment

                        • santosh

                          #13
                          Re: data types

                          koolj96...@yaho o.com wrote:
                          Then it's most probably a 32 bit object. What does sizeof(short)
                          indicate? Usually short is smaller than an int.
                          >
                          short is 2 bytes.
                          >
                          I guess I'll be using that. Originally I thought if it came out good,
                          I'd port myself a version to ubuntu, but now I don't really care, so
                          portability issues are less important now. I thought it'd be easier
                          to be portable in C.
                          You can still do the port by creating a typedef for whatever is two
                          bytes for the target system. Then all you have to do is edit the
                          typedef, (also be careful with I/O functions like printf and scanf).
                          Of course since most systems provide a stdint.h, (and even if they
                          don't it's easy enough to create your own), you can use the, arguably
                          more portable, types like uint16_t etc.

                          <snip>
                          Beware though that gcc by itself doesn't include the standard C
                          library. However your operating system should include a C library,
                          which gcc can use.
                          >
                          Would changing compilers cause unexpected problems? (logic or syntax
                          or something like that?)
                          If you use one compiler's header with another compiler, then most
                          certainly you'll have problems. However the system's C library can
                          usually be used with multiple compilers, if you're careful. But it's
                          better to use compatible versions of the standard library and the
                          compiler. For Linux it's glibc and gcc, and for Windows it's gcc and
                          MS's CRT. The latter combination has some problems with long long and
                          long double I/O.

                          Comment

                          • Keith Thompson

                            #14
                            Re: data types

                            koolj96825@yaho o.com writes:
                            [...]
                            Next, I was using them as keys and since some quirk in the compiler
                            didn't let me use the constant for a max unsigned int, I made my own
                            constant.
                            What exactly do you mean by "didn't let me use"? What did you try,
                            and what happened when you tried it?

                            If you have "#include <limits.h>" at the top of your source file,
                            UINT_MAX should give you the maximum value of type unsigned int. If
                            you do that right, and it doesn't work, your implementation is broken,
                            but it's far more likely you did something wrong.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • bluejack

                              #15
                              Re: data types

                              On Mar 3, 12:11 am, "santosh" <santosh....@gm ail.comwrote:
                              koolj96...@yaho o.com wrote:
                              Next, I was using them as keys and since some quirk in the compiler
                              didn't let me use the constant for a max unsigned int, I made my own
                              constant.
                              Your compiler is either *really* old or broken or both. I strongly
                              suggest replacing it.
                              Or he's not using it correctly.

                              KoolJ: what's your development environment?

                              -Bluejack

                              Comment

                              Working...