« Assigning Roles in Wordpress | Home | C If, Else if, Else Tutorial »
Which Variable Type Do I Use in C?
How should I decide which integer type to use?
If you might need large values (above 32,767 or below -32,767), use long. Otherwise, if space is very important (i.e. if there are large arrays or many structures), use short. Otherwise, use int. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of sign- extension problems when manipulating bits or bytes, use one of the corresponding unsigned types. (Beware when mixing signed and unsigned values in expressions, though.)
Although character types (especially unsigned char) can be used as “tiny” integers, doing so is sometimes more trouble than it’sworth, due to unpredictable sign extension and increased code size. (Using unsigned char can help)
A similar space/time tradeoff applies when deciding between float and double. None of the above rules apply if pointers to the variable must have a particular type.
If for some reason you need to declare something with an *exact* size (usually the only good reason for doing so is when attempting to conform to some externally-imposed storage layout), be sure to encapsulate the choice behind an appropriate typedef, such as those in C99’s

