473,569 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

short or int

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?
May 14 '06 #1
29 2647
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #2
Richard Heathfield wrote:
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.


Ok, then. But why is there two types if they both
have the same minimum? And why choose int instead of short?
May 14 '06 #3
In article <WW************ *******@newsb.t elia.net>, john <no@email.com >
writes
Richard Heathfield wrote:
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.


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


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.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

May 14 '06 #4
john wrote:

Richard Heathfield wrote:
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.


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.

--
pete
May 14 '06 #5
pete wrote:
john wrote:
Richard Heathfield wrote:
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.

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.


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.
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.


I understand, thanks
May 14 '06 #6
john wrote:
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.
May 14 '06 #7
john wrote:

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.c om, 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/googlegroupsrep ly/>
May 14 '06 #8
john wrote:
Richard Heathfield wrote:
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.


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
May 14 '06 #9
In article <44************ ***********@new s.optusnet.com. au>, Simon Biber
<ne**@ralmin.cc > writes
john wrote:
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.


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

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

May 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2249
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker http://www.xs4all.nl/~nd/dekkerware
99
9001
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled with many different compilers. I got compile errors when it used va_arg to fetch an argument of type short. That seemed a little strange to me, so I...
34
16635
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
8
1675
by: NilsNilsson | last post by:
I wrote this: short s1 = 0; short s2 = 1; short s3 = s1 + s2; And gor this compile error message: Cannot implicitly convert type 'int' to 'short' What is wrong here?
8
16365
by: Ken Dopierala Jr. | last post by:
Hi, I'm reading the header file of a PCX image and I need to convert 2 bytes to a short. How would I go about doing this? I know that they are bytes 8 & 9 in my byte array. I'm not sure how to take those two and convert them into a short though. In C I would just use a union and assign them accordingly. Thanks! Ken.
15
8224
by: Steffen Loringer | last post by:
Hi, I'm using the following function to join 2 char (byte) into one short on a 32 bit X86 platform: unsigned short joinUnsigShort(unsigned char a,unsigned char b) { unsigned short val = 0; val = a; val <<= 8;
4
5166
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a 'uint16_t short'. The compiler (GNU 3.3.5) allows this but ignores the original unsigned keyword and initialzes the variable as a signed short int. ...
10
5620
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl; } else
10
5427
by: nyhetsgrupper | last post by:
The following code result in the following compilation error: Error 1 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) short a = 1; short b = 2; short result = a + b; Can anyone explain why??
3
6390
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.