• Feed
  • About The Blog

    The Blogosaurus is a company technology blog from Louisville Web Development firm MIB Solutions. The issues on or blog range from our personal use of Wordpress, Joomla, PHP and Database programming, AJAX, Flash, and other web development languages and tools we use on our client's projects.

    The Blogosaurus was born in 2008, and continues to be a resources for Internet professionals and Google searches alike.

  • Categories

  • Archives

  • Recent Posts

  • Our Analytics


    9,527
    Unique
    Visitors
    Powered By Google Analytics
  • Look Around

  • Recent Posts

  • Categories

  • « Assigning Roles in Wordpress | Home | C If, Else if, Else Tutorial »

    Which Variable Type Do I Use in C?

    October 4, 2009

    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 .

    Comments