By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

data types

P: n/a
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

Mar 2 '07 #1
Share this Question
Share on Google+
21 Replies


P: n/a
ko********@yahoo.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 ]
Mar 2 '07 #2

P: n/a
Roberto Waltman wrote:
ko********@yahoo.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.
Mar 3 '07 #3

P: n/a
ko********@yahoo.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.

Mar 3 '07 #4

P: n/a
Roberto Waltman wrote:
ko********@yahoo.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>
Mar 3 '07 #5

P: n/a
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.
ko********@yahoo.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
Mar 3 '07 #6

P: n/a
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.

Mar 3 '07 #7

P: n/a
ko********@yahoo.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.

Mar 3 '07 #8

P: n/a

Rahul wrote:

Please don't top-post.
[fixed]
ko********@yahoo.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.

Mar 3 '07 #9

P: n/a

<ko********@yahoo.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.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 3 '07 #10

P: n/a
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?)

Mar 3 '07 #11

P: n/a
ko********@yahoo.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
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.
>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 "#ifincexists" to check if an include file
is available, but it does not.
--
Flash Gordon
Mar 3 '07 #12

P: n/a
koolj96...@yahoo.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.

Mar 3 '07 #13

P: n/a
ko********@yahoo.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_Keith) ks***@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"
Mar 3 '07 #14

P: n/a
On Mar 3, 12:11 am, "santosh" <santosh....@gmail.comwrote:
koolj96...@yahoo.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

Mar 3 '07 #15

P: n/a
>
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

Mar 3 '07 #16

P: n/a
Rahul wrote:
>
i just want to extend the topic a little further.....
.... snip ...

Rude top-posting is not likely to get you constructive replies.
See the following links.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)
Mar 3 '07 #17

P: n/a
ko********@yahoo.com wrote:

Or he's not using it correctly.
<snip>
KoolJ: what's your development environment?

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

<snip>
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 :)

Mar 4 '07 #18

P: n/a
ko********@yahoo.com wrote:
>
.... snip ...
>
Off on a tangent, are any of you "professional" programmers?
As in, you do it for a living.
I suspect most of the regulars are, or were.

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

P: n/a
<ko********@yahoo.comwrote:
>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

Mar 4 '07 #20

P: n/a
CBFalconer wrote, On 04/03/07 00:37:
ko********@yahoo.com wrote:
... snip ...
>Off on a tangent, are any of you "professional" programmers?
As in, you do it for a living.

I suspect most of the regulars are, or were.
So do I, and I suspect they are or where making a good living from it.
I've been in SW development professionally for over 20 years (although I
first use C in about 1995 I think). I think others here have been using
C professionally for rather longer that I have been working.
--
Flash Gordon
Mar 4 '07 #21

P: n/a
ko********@yahoo.com writes:
Off on a tangent, are any of you "professional" programmers? As in,
you do it for a living.
I do programming professionally, academically, recreationally,
and procrastinationally.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Mar 4 '07 #22

This discussion thread is closed

Replies have been disabled for this discussion.