Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > data types

Reply
Thread Tools

data types

 
 
koolj96825@yahoo.com
Guest
Posts: n/a
 
      03-02-2007
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

 
Reply With Quote
 
 
 
 
Roberto Waltman
Guest
Posts: n/a
 
      03-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 ]
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      03-03-2007

<(E-Mail Removed)> wrote 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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-03-2007
Roberto Waltman wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-03-2007
(E-Mail Removed) 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-03-2007
Roberto Waltman wrote:
> (E-Mail Removed) 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>


 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      03-03-2007
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.
(E-Mail Removed) 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


 
Reply With Quote
 
koolj96825@yahoo.com
Guest
Posts: n/a
 
      03-03-2007
> > 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-03-2007
(E-Mail Removed) 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-03-2007

Rahul wrote:

Please don't top-post.
[fixed]

> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
Can XSD simple types be derived from complex types? Soren Kuula XML 2 12-01-2005 07:51 PM
Where are ref types that are members of value types stored? Sathyaish ASP .Net 2 05-22-2005 07:32 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM
STD types vs C++ intrinsic types Jeremy Cowles C++ 5 08-19-2003 05:33 PM



Advertisments