Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Integer Types

Reply
Thread Tools

Integer Types

 
 
C man
Guest
Posts: n/a
 
      05-15-2009
Most ppl use int a lot, long some times, short very rarely, and char only
for Strings.

How should I decide which integer type to use?

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      05-15-2009
On May 15, 2009 19:01, in comp.lang.c, C man ((E-Mail Removed)) wrote:

> Most ppl use int a lot,


because int stores most reasonable values

> long some times,


because long stores very large values, and you sometimes need to store very
large values

> short very rarely,


usually because there is a semantic or storage need for a short integer
(semantic, to say that the integer value is within a limited range around
0, or storage in that short is sometimes realized in a smaller storage
usage than int is)

> and char only for Strings.


Nonsense. programs use char as a really short integer, or a single value of
the implementation's characterset. A char is never used "for strings";
an /array of char/ is explicitly used for "normal" strings, but you weren't
talking about arrays, you were talking about simple data types.

> How should I decide which integer type to use?


By the type of integer you need to store in it.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      05-16-2009
On May 15, 4:01*pm, C man <(E-Mail Removed)> wrote:
> Most ppl use int a lot, long some times, short very rarely, and char only
> for Strings.
>
> How should I decide which integer type to use?


Make life easy on yourself. Use int unless you KNOW why you should
use a different integer type.
 
Reply With Quote
 
C man
Guest
Posts: n/a
 
      05-16-2009
On Sat, 16 May 2009 01:12:57 -0700, Barry Schwarz wrote:
> On May 15, 4:01*pm, C man <(E-Mail Removed)> wrote:
>> Most ppl use int a lot, long some times, short very rarely, and char only
>> for Strings.
>>
>> How should I decide which integer type to use?

>
> Make life easy on yourself. Use int unless you KNOW why you should
> use a different integer type.


Thanks.

Wouldn't it just be better though if the sizes of the standard types were
precisely defined?


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2009
C man wrote:
> On Sat, 16 May 2009 01:12:57 -0700, Barry Schwarz wrote:
>> On May 15, 4:01 pm, C man <(E-Mail Removed)> wrote:
>>> Most ppl use int a lot, long some times, short very rarely, and char only
>>> for Strings.
>>>
>>> How should I decide which integer type to use?

>> Make life easy on yourself. Use int unless you KNOW why you should
>> use a different integer type.

>
> Thanks.
>
> Wouldn't it just be better though if the sizes of the standard types were
> precisely defined?


No, not all hardware is created equal!

--
Ian Collins
 
Reply With Quote
 
C man
Guest
Posts: n/a
 
      05-16-2009
On Sat, 16 May 2009 22:15:27 +1200, Ian Collins wrote:
> C man wrote:
>> On Sat, 16 May 2009 01:12:57 -0700, Barry Schwarz wrote:
>>> On May 15, 4:01 pm, C man <(E-Mail Removed)> wrote:
>>>> Most ppl use int a lot, long some times, short very rarely, and char only
>>>> for Strings.
>>>>
>>>> How should I decide which integer type to use?
>>> Make life easy on yourself. Use int unless you KNOW why you should
>>> use a different integer type.

>>
>> Thanks.
>>
>> Wouldn't it just be better though if the sizes of the standard types were
>> precisely defined?

>
> No, not all hardware is created equal!


OK, but then all that happens is that some people use typedefs like int16
and int32 and then define these typedefs to be int, short, long, etc.
depending on what machine they're using. Other people just use int etc.
and then their code breaks when they port it to a different machine.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2009
C man wrote:
> On Sat, 16 May 2009 22:15:27 +1200, Ian Collins wrote:
>> C man wrote:
>>> On Sat, 16 May 2009 01:12:57 -0700, Barry Schwarz wrote:
>>>> On May 15, 4:01 pm, C man <(E-Mail Removed)> wrote:
>>>>> Most ppl use int a lot, long some times, short very rarely, and char only
>>>>> for Strings.
>>>>>
>>>>> How should I decide which integer type to use?
>>>> Make life easy on yourself. Use int unless you KNOW why you should
>>>> use a different integer type.
>>> Thanks.
>>>
>>> Wouldn't it just be better though if the sizes of the standard types were
>>> precisely defined?

>> No, not all hardware is created equal!

>
> OK, but then all that happens is that some people use typedefs like int16
> and int32 and then define these typedefs to be int, short, long, etc.
> depending on what machine they're using. Other people just use int etc.
> and then their code breaks when they port it to a different machine.


That's one reason why we have standardised fixed width types.

If the code requires fixed width types, the only way to make it portable
is to use them. If they aren't required, don't use them.

--
Ian Collins
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-16-2009
C man wrote:
> Most ppl use int a lot, long some times, short very rarely, and char only
> for Strings.
>
> How should I decide which integer type to use?


u shd rtff <http://www.c-faq.com/decl/inttypes.html>.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
luserXtrog
Guest
Posts: n/a
 
      05-16-2009
On May 16, 5:17*am, Tetsuya <(E-Mail Removed)> wrote:
> Tetsuya wrote:
> > They depend. To see how big are those data types on your
> > computer+OS+compiler, run a simple program similar to this:

>
> Well, I forgot to return from main
>


You're covered. You did say "program similar to this".
Another variation would be to cast the result of sizeof
to int.

--
lxt
 
Reply With Quote
 
C man
Guest
Posts: n/a
 
      05-16-2009
On Sat, 16 May 2009 12:58:59 +0000, Richard Heathfield wrote:
> C man said:
>
> <snip>
>
>> Other
>> people just use int etc. and then their code breaks when they port
>> it to a different machine.

>
> The trick is to write it in such a way that it doesn't.


Well then what should the 64-bit type be on a machine that can support it?

 
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
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
Standard integer types vs <stdint.h> types euler70@gmail.com C Programming 163 01-28-2008 03:21 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments