• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

short, int, long in C/C++

Status
Not open for further replies.
Level 22
Joined
Aug 27, 2013
Messages
3,973
Hello, so I've been learning C & C++ (mainly C for now) still at the beginner phase.
I'm confused about what are the differences between these three and when to use them.
Is it simply a matter of range or size or what?

I'd like to ask about float, double, and long double as well but I think I could probably understand them if I understood the integer ones.

Thanks in advance.
 
Yeh you're right: C++ Data Types.

..so their size in memory and so also their allowed number range is different. "long" can be 4 bytes like an normal "int", but can be 8 bytes, too, on a different system: What is the difference between an int and a long in C++?

You can always use the sizeof() function (shown in linked article above) to get sure how much memory one type takes.

..when it's heading upto ~million persistent data table entries, or so, then it can make some good difference in memory, dependant on what data types are chosen.
 
Level 22
Joined
Aug 27, 2013
Messages
3,973
But in C, the range of int values is from -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
I inputted a value higher than 32,767 and it ran just fine, so my question is in what situation it accepts either value range? Or is it because of the compiler? I'm using Dev-C++ (to learn C, yeh)

Yeh you're right: C++ Data Types.
Imma readin' dis C Data Types
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,197
You can always use the sizeof() function (shown in linked article above) to get sure how much memory one type takes.
This does not necessarily reflect alignment requirements, hence if you pack a 4 byte type and a 1 byte type together in a struct you might find the result takes 8 bytes in memory or potentially more.

Outside of the standard memory footprint considerations, there are also performance considerations. Traditionally some of the types are not directly supported by a platforms underlying instruction set and so multiple instructions need to be executed to emulate the desired behaviour.

This is still noticeable when building an application targeting x86 rather than x86-64. The 8 byte types (either long or long long depending on platform) require such software level emulation on x86 since the maximum standard register and word size is only 32bits. This emulation may include multiple 4 byte memory read and write instructions, multiple chained together addition and subtraction instructions or even non-trivial subroutines for multiplication and division. The same 8 byte types on x86-64 are manipulated with just single instructions and outside of bandwidth differences take approximately the same time to execute as x86 does for 32bit types.

But in C, the range of int values is from -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
The range is determined by the number of bits (bytes) the compiler uses for the type. Now days it is usually 4 bytes so "-2,147,483,648 to 2,147,483,647". In the old days or on some platforms it is only 2 bytes so "-32,768 to 32,767". If you built an x86/x86-64 application then the range will likely be -2,147,483,648 to 2,147,483,647. If you built targeting an architecture where it is 2 bytes then inputting a value larger than 32,767 will likely produce a compile warning (loss of precision due to implicit conversion) and integer overflow would occur.

In modern C/C++ language revisions there are a set of standard header files containing macros, constants and typedefs that explicitly define the sizes and numeric ranges of the various types. These should be used rather than hard coding constant values or making assumptions.

If all you care about is numeric range then I strongly recommend using the sized integer types as those guarantee you specific numeric ranges.
Fixed width integer types (since C++11) - cppreference.com

For example with a performance critical application involving numbers in the range of a 32bit unsigned integer you might want to use uint_fast32_t so you are guaranteed the fastest type for processing them. If you want to pack lots of 64bit signed integer ranged values densely in memory you would use int_least64_t. Something like int32_t would be used for very specific platform coupled operations where it is known that a 32bit integer type is natively supported and that it is needed for interfacing with platform specific libraries, data or intrinsic functions or if one understands the architecture enough for explicit optimizations.
 
  • Like
Reactions: pyf
Hello, so I've been learning C & C++ (mainly C for now) still at the beginner phase.
I'm confused about what are the differences between these three and when to use them.
Is it simply a matter of range or size or what?

I'd like to ask about float, double, and long double as well but I think I could probably understand them if I understood the integer ones.

Thanks in advance.
@IcemanBo @Dr Super Good explained most if not all of the necessary details. If you are mostly on computer application programming, you should be safe using Int most of the time if memory isn't a big deal. These types get more important when you're playing with ATMega (embedded systems) due to their memory constraint.

Short is 2 bytes guaranteed.
Int is either 2 or 4 bytes.
Long is 4 bytes guaranteed.

There's also the signed and unsigned state. Signed is from negative, and Unsigned from 0 (start point).
 
Last edited:

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,197
Short is 2 bit guaranteed.
Int is either 2 or 4 bit.
Long is 4 bit guaranteed.
In the C++ standard...

Fundamental types - cppreference.com

int - basic integer type. The keyword int may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it's guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits (see below).
So it is 16 bits and not 2 or 4 bits...
short - target type will be optimized for space and will have width of at least 16 bits.
Again it is 16 bits and not 2 bits.
long - target type will have width of at least 32 bits.
And long is 32bits and not 4 bits.
 
Status
Not open for further replies.
Top