short or int

J

john

I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?
 
R

Richard Heathfield

john said:
I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?

short is not guaranteed to take less space, though.

short is at least 16 bits wide.
int is at least 16 bits wide.
 
J

john

Richard said:
john said:


short is not guaranteed to take less space, though.

short is at least 16 bits wide.
int is at least 16 bits wide.

Ok, then. But why is there two types if they both
have the same minimum? And why choose int instead of short?
 
C

Chris Hills

[QUOTE="john said:
john said:


short is not guaranteed to take less space, though.

short is at least 16 bits wide.
int is at least 16 bits wide.

Ok, then. But why is there two types if they both
have the same minimum? And why choose int instead of short?[/QUOTE]

Some machines have 32 bit ints and 8 bit chars so you man want a 16 bit
short.

In fact on any machine where the int is more than 16 bits (32, 64, 128
and others) you may want an integer between the int and char.
 
P

pete

john said:
Ok, then. But why is there two types if they both
have the same minimum?

It gives the compiler an option to have more variety.
short and int may be implemented with the same size and range
or they may be different.
And why choose int instead of short?

Expressions of type short,
get converted to type int in most operations, anyway,
so the meaning of the code is generally simpler
when short types aren't used.
 
C

CBFalconer

john said:
I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?

Because an int is the type that will produce the optimum code, in
terms of both speed and code space, on that machine. A short *may*
be used to reduce the data space required when it has sufficient
range. This is probably unwise unless you have a great many of
them to store.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

john

pete said:
It gives the compiler an option to have more variety.
short and int may be implemented with the same size and range
or they may be different.

But if they are different, is it portable to use int
for values greater than 32,767 ?
The FAQ says that a long should be used in that case.
Expressions of type short,
get converted to type int in most operations, anyway,
so the meaning of the code is generally simpler
when short types aren't used.

I understand, thanks
 
S

Simon Biber

john said:
I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?

If your 'int' only holds values up to 32,767, then it is surely the same
type as 'short' on your machine. There is no reason to use one over the
other. However, on most machines, 'int' can hold values over 32,767.

Using an int may be faster than using a short. The type int is designed
to represent the native word size of the computer, where possible.

On many 64-bit computers that is no longer the case. If they made 'int'
64 bits, and 'short' remained 16 bits, then there would be no 32-bit
integer type available. If 'short' moved to 32 bits, then there would be
no 16-bit integer type available. The usual solution is to leave 'char'
as 8 bits, 'short' as 16 bits, and 'int' as 32 bits. Most C
implementations on 64-bit hardware do move 'long' to 64 bits, while I
believe some leave 'long' as 32 bits and continue using 'long long' for
the 64 bit type.

The choice of what type to use when programming in C should usually be
based on what range of values you need to store. If 16 bits are
sufficient, use an 'int', if 32 bits are required, use a 'long', and if
64 bits are required, use a 'long long'.

Simon.
 
F

Flash Gordon

john said:
Ok, then. But why is there two types if they both
have the same minimum? And why choose int instead of short?

The idea of int is that it is the natural integer size of the processor
and so will be faster than short. So you use short if you are trying to
reduce space, but if space is not known to be a problem you use int.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
C

Chris Hills

Simon Biber said:
If your 'int' only holds values up to 32,767, then it is surely the same
type as 'short' on your machine. There is no reason to use one over the
other. However, on most machines, 'int' can hold values over 32,767.

Where do you get that idea. On the vast majority an int is 16 bits.
 
S

SM Ryan

# I read in the first question in the FAQ that both
# short and int are guaranteed to be able to hold
# values up to 32,767.
#
# Then why would one use an int instead of a short
# if short takes less space?

On some systems the minimum size of a variable is sizeof(int)
(due to optimal memory access with the hardware). Using a short
variable there would save no space. More generally, more and
more programs are run with enough vm available that
programmers rarely have to deal with things like a maximum
stack size of 65535 bytes.
 
J

Jordan Abel

But if they are different, is it portable to use int
for values greater than 32,767 ?

No - but int may be faster than short due to its larger size.

And there are other standards which guarantee an int of at least 32 bits
for systems which adhere to them.
 
C

Chris Hills

SM Ryan <wyrmwif@tango- said:
# I read in the first question in the FAQ that both
# short and int are guaranteed to be able to hold
# values up to 32,767.
#
# Then why would one use an int instead of a short
# if short takes less space?

On some systems the minimum size of a variable is sizeof(int)
(due to optimal memory access with the hardware). Using a short
variable there would save no space. More generally, more and
more programs are run with enough vm available that
programmers rarely have to deal with things like a maximum
stack size of 65535 bytes.

The vast majority of MCU in the world do not have VM and are 8 or 16 bit
systems. In fact the most common MCU in the world (ie it makes up about
1 in 3 processors out there) is an 8 bit system with 128 bytes (not
Kbytes) of RAM and a maximum code space of 64K bytes. (8 bit bytes :)

Also even on 32/64 or 128 bit machines you may want to access 16 bit
registers and use a short.
 
K

Keith Thompson

Chris Hills said:
Where do you get that idea. On the vast majority an int is 16 bits.

And where did you get *that* idea?

It's probably true that the vast majority of systems with C
implementations (counting physical CPUs) have 16-bit ints. It's also
true that the vast majority of CPUs are 8-bit embedded systems. (A
typical PC probably has more 8-bit CPUs than 32-bit or 64-bit CPUs, if
you count things like the keyboard controller.)

I believe that the vast majority of systems with hosted
implementations have 32-bit ints -- and these are the vast majority of
implementations that a newbie C programmer is likely to run into.

Saying that the "vast majority" of systems have 16-bit ints may be
technically correct, but it's misleading unless you qualify it.
 
C

Chris Hills

Keith Thompson <kst- said:
And where did you get *that* idea?

It's probably true that the vast majority of systems with C
implementations (counting physical CPUs) have 16-bit ints. It's also
true that the vast majority of CPUs are 8-bit embedded systems. (A
typical PC probably has more 8-bit CPUs than 32-bit or 64-bit CPUs, if
you count things like the keyboard controller.)

Yes. A PC is really just a collection of embedded systems. From the
keyboard controller to the Graphics processor.
I believe that the vast majority of systems with hosted
implementations have 32-bit ints -- and these are the vast majority of
implementations that a newbie C programmer is likely to run into.

Not really.... from my recent experience talking to UK universities (I
am assisting a publisher with a text book) C#, Java. C++ etc tends to be
taught in Computer science classes. They don't seem to bother much
with C. So PC and unix programmers will not be using C in the first
place. A few years a go when I gave a lecture to a UK university I
discovered that CS students did nto know what a terminal windows or dos
box was!!! they only knew the GUI interface. Though with the rise of
Linux I hope more have an appreciation of UNIX type systems

It seems C is mainly taught in the electronics departments doing
embedded work wher e8 or 16 bit systems are the norm. Mind you in a year
or three it will probably be the 32bti ARM parts they all use.

In any event the short is there for when you need something between
char and int and not all systems have megabytes of memory. Though I have
been told that for various reasons one company uses long for ALL
variables!
 
P

pete

john wrote:
But if they are different, is it portable to use int
for values greater than 32,767 ?
No.

The FAQ says that a long should be used in that case.

If you want maximum portabilty,
and if you're going to assign a value that
takes more than 16 bits to represent,
then it doesn't make sense to use a type
that is only guaranteed to have at least 16 bits.
long is guaranteed to have at least 32 bits.
 
D

Dave Vandervies

Simon Biber said:
On many 64-bit computers that is no longer the case. If they made 'int'
64 bits, and 'short' remained 16 bits, then there would be no 32-bit
integer type available. If 'short' moved to 32 bits, then there would be
no 16-bit integer type available. The usual solution is to leave 'char'
as 8 bits, 'short' as 16 bits, and 'int' as 32 bits. Most C
implementations on 64-bit hardware do move 'long' to 64 bits, while I
believe some leave 'long' as 32 bits and continue using 'long long' for
the 64 bit type.

If the native word size is 64 bits, wouldn't it make more sense to do
something like this?

short short 16 bits
short 32 bits
int 64 bits
long 64 bits
long long 128 bits


dave
(not entirely facetious)
 
H

Herbert Rosenau

I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?

On an 32 bit system you would use
- char if the value is in range if 8 bit
- short 16
- int 32
- long 32 *
- long long 32 **

* on a 64 bit system it would be 32 or 64 bit
** 64 or 128 bit

On a 16 bit system int will be 16 bit, long 32 bit and long long when
exists 32 bit.

So with the decision to use int as 32 bit you will decide that your
app will never run on 16 bit systems.

But as technicque goes forward 16 bit systems are not really outdated
but for a wide spectrum of usage really not usuaal today. The same is
currently with 32 bit systems, they are replaced successive with 64
bit systems. You has to decide which system is the minimal base your
program you writes today is the one that should be used. Often enough
you'll be limited to 8 bit systems where C extends to have 16 bit data
(short/int) to have a practical fixed point value stored. More often
you'll have 16 bit systems with not even more than 64 KB (sic) memory
available.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
I

Ian Collins

john said:
I read in the first question in the FAQ that both
short and int are guaranteed to be able to hold
values up to 32,767.

Then why would one use an int instead of a short
if short takes less space?

As no one else has mentioned them, if your system has the C99 types, you
can use (u)int_fast16_t and friends if you require a minimum number of
bits for a counter.
 
S

SM Ryan

# The vast majority of MCU in the world do not have VM and are 8 or 16 bit

How many use Ada or assembly instead of C?
 

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
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top