C
C man
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?
for Strings.
How should I decide which integer type to use?
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?
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.
C said: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!
C said: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.
C said: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?
Well, I forgot to return from main
C man said:
The trick is to write it in such a way that it doesn't.
C said:Well then what should the 64-bit type be on a machine that can support it?
Oops, I answered the wrong question...C said:Well then what should the 64-bit type be on a machine that can support it?
C man wrote:
snip
They depend. To see how big are those data types on your
computer+OS+compiler, run a simple program similar to this:
#include <stdio.h>
int main (void) {
printf ("char: %d byte\n", sizeof (char));
printf ("short int: %d byte\n", sizeof (short));
printf ("int: %d byte\n", sizeof (int));
printf ("long int: %d byte\n", sizeof (long));
printf ("long long int: %d byte\n", sizeof (long long));
}
C said:Thanks.
Wouldn't it just be better though if the sizes of the standard
types were precisely defined?
C said:.... snip ...
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.
C said:Well then what should the 64-bit type be on a machine that can
support it?
CBFalconer said:They are. Just read limits.h
Richard Heathfield said:Better to read the Standard. But the Standard does *not* precisely
define the sizes of the standard types, at least not the ones I
think he means (char * 3, short * 2, int * 2, long * 2, and perhaps
long long * 2); it only specifies *minimum* widths. It leaves
implementations to use any width they like that meets *or* exceeds
those minima.
That's good, as it allows for future improvements in hardware, which
the fixed-width types introduced by C99 do not.
Richard Heathfield said:Keith Thompson said:
Er, no, not really. What I meant - and I am beginning to suspect
that I didn't think it through properly, but let's see where it
goes anyway - was that if you nail your code to an exact-width type
such as those provided by C99, you may have to pay a performance
penalty when moving your code to platforms where a wider type is
available and better suited to the hardware.
C said:Well then what should the 64-bit type be on a machine that can support it?
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.