data types

K

koolj96825

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
 
R

Roberto Waltman

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

Malcolm McLean

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

Ian Collins

Roberto said:
... 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>.
 
S

santosh

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

CBFalconer

Roberto said:
... 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.
 
R

Rahul

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

koolj96825

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

santosh

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

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

santosh

Rahul wrote:

Please don't top-post.
[fixed]
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.
 
F

Flash Gordon

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).
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.
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.
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
http://clc-wiki.net/wiki/C_resources:IDEs

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.
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 "#ifincexists" to check if an include file
is available, but it does not.
 
K

koolj96825

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


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.


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?

Bit field? That may have been the proper term. I may have misspoken.
Sorry.
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?)
 
S

santosh

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.

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

Keith Thompson

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

bluejack

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
 
K

koolj96825

Or he's not using it correctly.

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.
KoolJ: what's your development environment?

I use open watcom 1.3
-Bluejack


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,

Jason
 
C

CBFalconer

S

santosh

I use open watcom 1.3

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

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

Yes, most here are.

My job is in bioinformatics, but I enjoy programming as an hobby,
during weekends and late nights :)
 
B

Beej Jorgensen

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

Games for a while, but now I'm taking time off to program for fun. :)

-Beej
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top