C++ Variables and Data Types

What are variables? Variables are like shortcuts. Nobody wants to type the number "3824473021" hundreds of times in a program, so we make shortcuts. A variable is really a sequence of memory which can store data and can be called back by using the variable name.

Data Types

First off, a data type tells a variable what kind of data it will be holding. The whole point of data types, is so that the computer can store just the right amount of bytes for a variable, instead of too much or too little. You must know the different data types to pick the one that best fits your needs.

toc_collapse=0;
Contents 

Here or the most common types you will encounter when programming:

bool = true or false

int = integer
-signed: -2147483648 to 2147483647
-unsigned: 0 to 4294967295

char = character
-signed: -128 to 127
-unsigned: 0 to 255

string = string (when #include <string>)

short int = short integer
-signed: -32768 to 32767
-unsigned: 0 to 65535

long int = long integer
-signed: -2147483648 to 2147483647
-unsigned: 0 to 4294967295

float = Floating point number
double = Double precision floating point number
long double = Long double precision floating point number

Declaring variables

Read about constants. Since we have a data type, pick a name for your variable. Try to link it with what the variable will be holding and what type of data it's holding. For example, I have a integer that tells me how many coins I have. I would name it intCoins. The whole point of this is so that the code can be easily read, and you don't have to keep looking to see what you named that variable.
To declare a variable, you type the data type, the name, and then (if you wish) you can set it a value. Here are two examples:

int intNUM = 1; //variable declared with starting value 1
int intNUM2; //variable declared with undeclared value

But, why have that take up two lines when we can make it one? You can declare multiple variables of the same type by separating them with commas. Like so:
int Num1, Num2, Num3 //creates 3 integers with names Num1, Num2, and Num3.

Scope

Scope is the visibility of a variable. For example, local scope means a variable declared inside a function, and outside of this function it is invisible. A global variable has global scope, meaning the whole program can see the variable.

Global Variables

Global variables are variables that are usable and editable through your whole program. Because they're not linked to a specific function, they are usable anywhere. To make your variable global, simply place it outside any type of function. An example is #include , is outside a function.

Typedef Variables

Typedef is a c++ keyword that allows you to use alias' within your application. These are not complicated. Typedef makes a variable with a data type, then you can later make an alias to that variable. Here's example code:

#include <string>
#include <iostream>

using namespace std;

//typedef placed after std
typedef string Stringer; //Creates string Stringer
typedef unsigned int Inter; //Creates integer Inter [unsigned part is optional]

int main(int argc, char *argv[])
{
    Stringer irString = "Hello World"; //Stringer makes alias irString to equal hello world
    Inter x = 5; //Inter makes alias x to equal 5
    cout << irString << " " << x; //displays the values of the alias'
    getchar();
    return 0;
}

Think of it as if a new (using code above) string is created called irString, with the attributes of Stringer.

Function Variables(Parameters)

Functions can also have variables called parameters. To learn more about these I suggest you look Here for more information.

Classes

Another type is also a class. To make a class you add class before your desired variable. Like so:

class MyClass {
//class code here
void MyClass(){
  cout << "MyClass Constructor";
}
};

I will not explain everything you can do in/with a class, but for further information go here.

Downside to variables

The big downside to variables is that they take up memory. In simple applications this isn't a big deal, but in applications where how much memory your application is taking up is a big deal, variables are a big deal and should be avoided whenever possible. They take up so much memory because the application reserves the amount of memory needed for the given data type. Example is that a double takes up 8 bytes of memory whether or not it has a value.

Al's picture

Ah the typedef stuff is good

Ah the typedef stuff is good to learn I suppose. Although I never used it yet.

Baran Ornarli's picture

Yeah, using typedef can

Yeah, using typedef can really help you in big projects especially. It's better than constantly typing long keywords too.

typedef unsigned int uint;

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.