short int in C

Discussion about everything. New games, 3d math, development tips...
trunks14
Posts: 45
Joined: Fri Dec 17, 2004 7:30 am
Location: America

short int in C

Post by trunks14 »

My teacher insists on saying that a short int in C is going to be 2 bytes 'long' in all architectures, I could have sworn I have read many many times that C ints are in no way standar in all architectures.... some of you may have a 64 bit computer/compiler, whats the length of a short int?
Not yet.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

ANSI requires short ints to be at least 16 bits, and no longer than ints. There's no express or implied maximum size for short int; you have to check <limits.h>

If he's making the statement, it's presumably up to him to convince you with evidence from the spec. C99 introduced the <stdint.h> fixed width integers precisely so people could avoid these unproductive debates.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

rogerborg wrote:C99 introduced the <stdint.h> fixed width integers precisely so people could avoid these unproductive debates.
Are those supported by VS by now? I'm still on VS 2005 where those had been missing (6 years was probably just not enough time to add something as complicated as new integer types...)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

CuteAlien wrote:
rogerborg wrote:C99 introduced the <stdint.h> fixed width integers precisely so people could avoid these unproductive debates.
Are those supported by VS by now? I'm still on VS 2005 where those had been missing (6 years was probably just not enough time to add something as complicated as new integer types...)
LOL..

I'm using VC9. Tell me where to check and I'll post here what you need.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Post by dejai »

Integer variables are a very confusing issue for newcomers to the C/C++ language or to any programmer whom have only required the use of one compiler on a individual platform. The <limits.h> or <climits> in newer C++ compilers provides information about integer types that may be used by your program at run time. Yet a C++ compiler is not required to include <limits.h>, yet that is not to say the gross majority do not in fact I cannot think of a single compiler that would not include <limits.h> unless having an extremely good reason for doing so.

I would recommend searching through the manual pages or documentation of your compiler as each compiler must document its implementation. I know this may seem like quite a tedious activity but it may be something worth looking into on a rainy afternoon. For example I believe the compiler behind Dev C++ has the same size for a int and long variable.

I hope this information has been some help to you. If you would like a useful resource I used while composing this post I would recommend taking a look at: http://home.att.net/~jackklein/c/inttypes.html

Thanks.
Programming Blog: http://www.uberwolf.com
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I just searched around a little and found some more info on stdint.h on the wiki: http://en.wikipedia.org/wiki/Stdint.h

Implementations for VS are offered by now from external parties. Though it does mention some problems with printf (and maybe the same with streaming?) so the new types should probably still be used with some care.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
trunks14
Posts: 45
Joined: Fri Dec 17, 2004 7:30 am
Location: America

Post by trunks14 »

Does any of you have a 64 bit computer & compiler? Could you tell me what your limits.h says for short int?
Not yet.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

shorts are also 16bit on typical 64bit machines. But they are not required to be of that size. However, I thought that they even need to be 16bit wide, just at least as large as a char (or whichever type they are ordered with).
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Hmm, you're right; I can't find a minimum size for short int in C99. K&R ANSI C guaranteed at least 16 bits.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

rogerborg was WRONG??! o.O
NNOOOOOOOOOOOOOOOOOOOOooo...oooo..oooo ................ why? *whisper* why...
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'd have to check the documents , but I'm pretty sure rogerborg is right. I think that C89 and C99 required that a short be able to represent -32767 to 32767 and that it be able to represent all of the values represented by a char. The first requirement implies at least 16-bits of representation, but the actual size of the type could be larger. It wouldn't be efficient, but it would be compliant.

Travis
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Ah, that's it. C99 5.2.4.2 specifies minimum magnitudes for limits.h, and SHRT_MIN / SHRT_MAX are indeed -32767 and +32767.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

The day is SAVED! :)

-Edit-

Bu... but, does that mean you were wrong about being wrong :?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Being wrong is an opportunity to learn. Learning is what separates us from the animals, or Republicans.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
switch_case
Posts: 34
Joined: Sat Mar 08, 2008 12:46 pm
Location: Germany, FFM
Contact:

Post by switch_case »

...,or Republicans.
so true.
if life could only be written in C++...
so much features, money++, remove_childs(), choose parents, life = new life, and no more <IDIOT> templates !
if you're looking for an nerdy coding language: http://lolcode.com/
Post Reply