C++ Variables and Data Types

In this C++ programming tutorial we take a look at variables and data types.


If you declare a variable in C++ (later on we will talk about how to do this), you ask the operating system for
a piece of memory. You (can) give this piece of memory a name and you can store something in that piece of memory (for later use).

The name of a variable is called an identifier. You can give a variable any name you want, as long as it is a valid identifier.

Valid identifier

A valid identifier is a sequence of one or more letters, digits or underscores and the identifier must begin with a letter. (It is not possible to start an identifier with a number.) It is also not possible to use punctuation marks, symbols or spaces in an identifier. Compiler specific keywords or external identifiers usually begin with an underscore. (It possible to use an underscore at the beginning of your identifier, but it is not recommended).

The C++ language is a “case sensitive” language. This means that an identifier with capital letters is not the same as with normal letters.

For example: myvar, Myvar, MyVar and MYVAR are four different variable identifiers.

The C++ language also uses a set of names that are reserved. It is not allowed to use these keywords as variable identifiers (variable names). The standard reserved keywords are:

  • asm, auto, bool, break, case, catch, class, char, const
  • const_cast, continue, default, delete, double, do, dynamic_cast
  • enum, else, explicit, extern, export, float, false, for, friend, goto
  • int, if, inline, long, mutable, namespace, new, operator, private
  • protected, public, register, reinterpret_cast, return, short, switch
  • signed, sizeof, static, static_cast, struct, template, this, throw
  • true, try, typedef, typeid, typename, union, unsigned, using, void
  • virtual, volatile, while, wchar_t

Data types

There are three types of variables: numeric-integer, numeric-real and character.

  • Numeric variables can either be of the type integer (int) or of the type real (float). Integer (int) values are whole numbers (like 10 or -10.) Real (float) values can have a decimal point in them. (Like 1.25 or
  • Character variables are letters of the alphabet, ASCII characters or numbers (0-9). If you declare a character variable you must always put the character between single quotes (like so ‘a’). So remember a character between single quotes is not the same as a character with single quotes.

The basic fundamental data types in the C++ language are (Note: size* are in bytes):





bool Boolean value. It can
take one of two values: true or false.
1 true or
char Character or small
1 signed: -128 to
unsigned: 0 to 255

2 signed: -32768 to
unsigned: 0 to 65535
wchar_t Wide
2 1 wide
int Integer. 4 signed: -2147483648 to
unsigned: 0 to 4294967295

4 signed: -2147483648 to
unsigned: 0 to 94967295
float Floating point
4 3.4e +/- 38 (7
double Double precision
floating point number.
8 1.7e +/- 308 (15
Long double precision
floating point number.
8 1.7e +/- 308 (15

Note: The values are those found on most 32bits systems, but remember the values can vary on different architecture (systems).

Declaring a variable

So we now know different data types and which rules an identifier must comply from this source. But how do we declare them. Declaring a variable is very easy. First you have to declare the data type. After the data type you place the name of the variable. But remember choose the names wisely. It is easier if a variable name reflects the use of that variable.

(For instance: if you name a float PI, you always know what it means).

Now let’s declare some variables, a variable MyIntegerVariable and MyCharacterVariable:

        int MyIntegerVariable;
	int MyCharacterVariable;

It is possible to declare more than one variable at the same time:

        int Variable1, Variable2, Variable3;
	int abc, def, ghi;

Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can be either negative or
positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable.
To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.

        unsigned int MyOnlyPositiveVar;
	signed int MyNegativeAndPositiveVar;
	int MyNegativeAndPositiveVar;

Calculations and variables

There are different operators that can be used for calculations which are listed in the following table:











Modulus(Remainder of
integer division)

Initialization of variables

When declaring a regular local variable, its value is by default undetermined. It is possible to store a concrete value in a variable at the same moment that the variable is declared. If you do this you are initializing a variable.

This can be done in two ways:

  1. The first method is the C-like method : int a = 0;
    (type identifier = initial value;)
  2. The second method is known as the constructor initialization: int a (0);
    (type identifier (inital value);)

Both ways of initialization are valid and equivalent in the C++ language.

Let’s combine all paragraphs in an example:

	using namespace std;

	int main ()
		int A = 15;
		int b(12);
		int Result; 

		A = A + 3;
		Result = A - b; 

		cout << Result; 

		return 0;

That’s all for now. In the next tutorial we will take a look at constants.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 12 responses to “C++ Variables and Data Types”

Why not let us know what you think by adding your own comment!

  1. ysuman on July 4th, 2011:

    good website…thanks

  2. rohit on August 3rd, 2011:

    good website thanks

  3. terry on October 4th, 2011:

    Well done !

    Thnaks very much

  4. Ricardo on October 9th, 2011:

    This is clear and concise. Works great coupled with classroom lessons. Appreciate the website very much! (test coming up)

  5. anna on May 21st, 2012:

    thank u and feel lucky found tis site…keep it up

  6. Freddy on August 2nd, 2012:

    Thanx 4 d help,m now clear!

  7. hina on December 14th, 2012:

    This site is very nice and helpful for-understanding . I request to the tutor plz publish all the program whether small and large programs related to every topic.

  8. love khan on March 27th, 2013:

    realllly help full for me in my engineering field thanks to the owner

  9. Vong Visalsambath on July 2nd, 2013:

    Helpful! thx 🙂

  10. kellking on September 18th, 2013:

    indeed helpful

  11. Mohamed rizwan on April 24th, 2014:

    It’s very helpful….

  12. Norbert on May 8th, 2014:

    Was reading variables and data types somewhere else and couldnt understand but this site has cleared it for me. Thanks editor. Keep up the good work.