Intro To C++/Variables

From Wikibooks, open books for an open world
Jump to: navigation, search

Variables. Just the name of it can bring you back to that daunting freshman year of high school (algebra anyone?). So the basic concept is when you create a variable, you are creating or reserving a "storage bin" for your program. And you can put stuff inside that storage bin. (From now on, when I mention storage bins, I mean variables, and vice-versa). But there are labels on each variable you create (from now on, declare). Just how you wouldn't put Christmas decorations in a storage bin called old magazines, you wouldn't put words in an integer. So here are the basic variable types you need to know (at least for this tutorial):

char - a character such as a, or 7, or ). Used as char myChar = '&';

int - a number from -2147483648 to 2147483647. Used as int myInt = 2000000000;(2 billion)

string - has to be included first (#include <string>). Used as string myString = "This is my string!!1!!";

bool - (boolean) true or false. Used as bool myBool = false;

float - decimal version of int. Range is from (try to stay with me here) 1.175494351 E – 38 to 3.402823466 E + 38. Used as float myFloat = 0.0283;

double - double precision version of a float (more decimal powers!). Range is from 2.2250738585072014 E – 308 to 1.7976931348623158 E + 308. Used like a float, but declared as double.

So, to create your own variable, you first have to tell the compiler what type it is. So let's create a string and output it (print it). Just delete everything in main(), and add this

string myString = "This will be ";
string myString2 = "on the screen."
cout<<myString;
cout<<myString2;
cout<<endl;
getch();
return 0;

When compiled and ran, this will display This will be on the screen.

But take a look at this code!:

string myString = "This will be ";
string myString2 = "on the screen."
cout<<myString<<myString2<<endl;
getch();
return 0;

Notice how I didn't create a new line of code to display myString2 and the endl. By adding << between each variable and/or string of text, it will make your code appear shorter, and for many people, look more organized. So when you compile and run it, it will say the same exact thing as if we had separate lines for each cout we used.\n Now say we want to display an integer variable. Take a look at this code:

int myNumber = 27;
cout<<myNumber;
myNumber++;
cout<<myNumber;

Here, there are 2 things you should have noticed. One being the basic one is you cout-ed a number by calling its variable. Well, that's one of the major uses of variables, and why they help programming so much. The next thing to look at is myNumber++;. What that means is add 1 to the value of myNumber. It looks a lot more neater than myNumber=myNumber+1;. Now your output should look like: 2728
Notice how the numbers are together though? You still need to end the line with variables.

One thing people may get confused with is the name of the variable. For all intensive purposes, i could have named myNumber to iL1k3pi3, and it would still function as an int. You could also name it myString, and as long as you declare it as an integer, it will still be an integer.


This page is being worked on.