How to Use Specific Variable Types in C - dummies

How to Use Specific Variable Types in C

By Dan Gookin

The C language’s variable types must be specific. Depending on the information stored, you may want to use one of these more detailed variable declarations. Here is a buffet of C language variable types and also the range of values those types can store.

Type Value Range printf() Conversion Character
_Bool 0 to 1 %d
char –128 to 127 %c
unsigned char 0 to 255 %u
short int –32,768 to 32,767 %d
unsigned short int 0 to 65,535 %u
int –2,147,483,648 to 2,147,483,647 %d
unsigned int 0 to 4,294,967,295 %u
long int –2,147,483,648 to 2,147,483,647 %ld
unsigned long int 0 to 4,294,967,295 %lu
float 1.17×10–38 to 3.40×1038 %f
double 2.22×10–308 to

The value range specifies the size of the number you can store in a variable as well as whether negative numbers are allowed. The compiler may not always flag warnings that happen when you assign the wrong value to a variable type. So get it right when you declare the variable!

For example, if you need to store the value -10, you use a short int, int, or long int variable. You cannot use an unsigned int, as this source code demonstrates.


#include <stdio.h>
int main()
  unsigned int ono;
  ono = -10;
  printf("The value of ono is %u.n",ono);

Exercise 1: Create a project named ex0607, and type the source code shown in Oh, No – an Unsigned int. Note that the %u conversion character is used for unsigned integer values. Build and run.

Here’s the output:

The value of ono is 4294967286.

The moral of the story: If your integer variable stores negative numbers, you can’t use an unsigned variable type.

  • The range of the int may be the same as the range of the short int on some compilers. When in doubt, use a long int.

  • You can specify long instead of long int.

  • You can specify short instead of short int.

  • The keyword signed can be used before any of the int variable types, as in signed short int for a short int, although it’s not necessary.

  • The void variable type also exists, although it’s used to declare functions that return no values. Still, it’s a valid variable type, though you’ll probably never use it to declare a variable.