C++ Variables and Data Types
In this C++ programming tutorial we take a look at variables and data types.
Variables
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
-1.25.) - 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):
Name |
Description |
Size* |
Range* |
bool | Boolean value. It can take one of two values: true or false. |
1 | true or false |
char | Character or small integer. |
1 | signed: -128 to 127 unsigned: 0 to 255 |
short int (short) |
Short Integer. |
2 | signed: -32768 to 32767 unsigned: 0 to 65535 |
wchar_t | Wide character. |
2 | 1 wide character |
int | Integer. | 4 | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
long int (long) |
Long integer. |
4 | signed: -2147483648 to 2147483647 unsigned: 0 to 94967295 |
float | Floating point number. |
4 | 3.4e +/- 38 (7 digits) |
double | Double precision floating point number. |
8 | 1.7e +/- 308 (15 digits) |
long double |
Long double precision floating point number. |
8 | 1.7e +/- 308 (15 digits) |
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:
Operator |
Operation |
+ |
Addition |
– |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
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:
- The first method is the C-like method : int a = 0;
(type identifier = initial value;) - 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:
#include<iostream>
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.
good website…thanks
good website thanks
Well done !
Thnaks very much
This is clear and concise. Works great coupled with classroom lessons. Appreciate the website very much! (test coming up)
thank u and feel lucky found tis site…keep it up
Thanx 4 d help,m now clear!
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.
realllly help full for me in my engineering field thanks to the owner
Helpful! thx 🙂
indeed helpful
It’s very helpful….
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.