Is it a good practice to use smaller data types for variables to save memory?

Posted by ThePlan on Programmers See other posts from Programmers or by ThePlan
Published on 2012-04-17T07:05:50Z Indexed on 2012/10/26 23:16 UTC
Read the original article Hit count: 265

Filed under:
|

When I learned the C++ language for the first time I learned that besides int, float etc, smaller or bigger versions of these data types existed within the language. For example I could call a variable x

int x;
or 
short int x;

The main difference being that short int takes 2 bytes of memory while int takes 4 bytes, and short int has a lesser value, but we could also call this to make it even smaller:

int x;
short int x;
unsigned short int x;

which is even more restrictive.

My question here is if it's a good practice to use separate data types according to what values your variable take within the program. Is it a good idea to always declare variables according to these data types?

© Programmers or respective owner

Related posts about c++

Related posts about data-structures