Declare Variable Types in C++

By Stephen R. Davis

In C++, variables must be declared and they must be assigned a type. Fortunately, C++ provides a number of variable types. See the table for a list of variables, their advantages, and limitations.

Common C++ Variable Types
Variable Defining a Constant What It Is
int 1 A simple counting number, either positive or negative.
short int A potentially smaller version of int.
It uses less memory but has a smaller range.
long int 10L A potentially larger version of int.
There is no difference between long and
int with gcc
long long int 10LL A potentially even larger version of int.
float 1.0F A single precision real number. This smaller version takes less
memory than a double but has less
accuracy and a smaller range.
double 1.0 A standard floating-point variable.
long double A potentially larger floating-point number. On the PC, long double is used for the native size of the
80×86 floating-point processor, which is 80 bits.
char ‘c’ A single char variable stores a
single alphabetic or digital character. Not suitable for
wchar_t L’c’ A larger character capable of storing symbols with larger
character sets like Chinese.
char string “this is a string” A string of characters forms a sentence or phrase.
bool true The only other value is false.

The long long int and long double were officially introduced with C++ ‘11.

The integer types come in both signed and unsigned versions. Signed is always the default (for everything except char and wchar_t). The unsigned version is created by adding the keyword unsigned in front of the type in the declaration. The unsigned constants include a U or u in their type designation. Thus, the following declares an unsigned int variable and assigns it the value 10:

unsigned int uVariable;
uVariable = 10U;

The following statement declares the two variables lVariable1 and lVariable2 as type long int and sets them equal to the value 1, while dVariable is a double set to the value 1.0. Notice in the declaration of lVariable2 that the int is assumed and can be left off:

// declare two long int variables and set them to 1
long int lVariable1
long lVariable2;     // int is assumed
lVariable1 = lVariable2 = 1;
// declare a variable of type double and set it to 1.0
double dVariable; dVariable = 1.0;

You can declare a variable and initialize it in the same statement:

int nVariable = 1;  // declare a variable and
             // initialize it to 1

A char variable can hold a single character; a character string (which isn’t really a variable type but works like one for most purposes) holds a string of characters. Thus, ‘C’ is a char that contains the character C, whereas “C” is a string with one character in it.

A rough analogy is that a ‘C’ corresponds to a nail in your hand, whereas “C” corresponds to a nail gun with one nail left in the magazine.

If an application requires a string, you’ve gotta provide one, even if the string contains only a single character. Providing nothing but the character just won’t do the job.