data types

Discussion in 'C Programming' started by koolj96825, Mar 2, 2007.

  1. koolj96825

    koolj96825 Guest


    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
    koolj96825, Mar 2, 2007
    1. Advertisements

  2. 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.

    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Mar 2, 2007
    1. Advertisements

  3. 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.
    Malcolm McLean, Mar 3, 2007
  4. koolj96825

    Ian Collins Guest

    int16_t would be better, to match the types in <stdint.h>.
    Ian Collins, Mar 3, 2007
  5. koolj96825

    santosh Guest

    For a particular C implementation an int is (sizeof(int) * CHAR_BIT)
    bits. For modern PCs it's often 32 or 64.
    Could be.
    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

    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.
    santosh, Mar 3, 2007
  6. koolj96825

    CBFalconer Guest

    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.
    CBFalconer, Mar 3, 2007
  7. koolj96825

    Rahul Guest

    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?

    Rahul, Mar 3, 2007
  8. koolj96825

    koolj96825 Guest

    16-bit. It may be out of date.
    Yes, I think the manual was not kept up.
    sizeof tells me there are 4 bytes to an int.
    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.

    I have now implemented more typedefs for this.

    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.

    I will need to google this. Thank you.
    koolj96825, Mar 3, 2007
  9. koolj96825

    santosh Guest

    Then it's most probably a 32 bit object. What does sizeof(short)
    indicate? Usually short is smaller than an int.
    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.
    Your compiler is either *really* old or broken or both. I strongly
    suggest replacing it.
    If it is equal to (2^n) - 1, where n is the number of value bits in
    your object, then it should.
    Have you considered a bitfield as an alternative?

    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.
    santosh, Mar 3, 2007
  10. koolj96825

    santosh Guest

    Rahul wrote:

    Please don't top-post.
    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.
    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.
    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.
    santosh, Mar 3, 2007
  11. koolj96825

    Flash Gordon Guest

    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).
    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.
    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!
    Unsigned integer types are guaranteed by the C standard to wrap.
    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.
    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.
    They are good suggestions. There are also various IDEs using gcc. I
    suggest you look at 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 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 "#ifincexists" to check if an include file
    is available, but it does not.
    Flash Gordon, Mar 3, 2007
  12. koolj96825

    koolj96825 Guest

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

    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.

    Bit field? That may have been the proper term. I may have misspoken.
    Would changing compilers cause unexpected problems? (logic or syntax
    or something like that?)
    koolj96825, Mar 3, 2007
  13. koolj96825

    santosh Guest

    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.

    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.
    santosh, Mar 3, 2007
  14. 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, Mar 3, 2007
  15. koolj96825

    bluejack Guest

    Or he's not using it correctly.

    KoolJ: what's your development environment?

    bluejack, Mar 3, 2007
  16. koolj96825

    koolj96825 Guest

    I may not have been using it correctly. This is true, since it worked
    in one part of my code and not in another, I suspect perhaps I did
    something wrong. I used the constant in a comparison early in my code
    and I think it worked, (I didn't test the drop out at that number),
    but later I used the same constant to calculate how many bytes I'd
    need for my bit map and the compiler would drop me an error that I
    couldn't figure out so I just defined my own.
    I use open watcom 1.3

    After a good night sleep, I've decided you are all correct in that I
    should rewite my code to not depend on the integer being a certian
    size. Thank you all for such great help.

    Off on a tangent, are any of you "professional" programmers? As in,
    you do it for a living.

    Best Reguards,

    koolj96825, Mar 3, 2007
  17. koolj96825

    CBFalconer Guest

    CBFalconer, Mar 3, 2007
  18. koolj96825

    CBFalconer Guest

    I suspect most of the regulars are, or were.
    CBFalconer, Mar 4, 2007
  19. koolj96825

    santosh Guest

    I notice from the Open Watcom website that the latest release is 1.6.
    Do upgrade to it.

    Yes, most here are.

    My job is in bioinformatics, but I enjoy programming as an hobby,
    during weekends and late nights :)
    santosh, Mar 4, 2007
  20. Games for a while, but now I'm taking time off to program for fun. :)

    Beej Jorgensen, Mar 4, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.