This tutorial is part of a Collection: C++ Tutorial
rate up
3
rate down
1596
views
bookmark
04. Variables

In this tutorial we will learn what are variables and how to use them in C++.

There are no files for this tutorial
##Variables and types## In the previous tutorial we created a program where we wrote several lines of code to output something to the screen. It certanly would be better if we typed the output sentance ourselves when the program starts. However programming isn't just limited to outputing stuff to the screen. If we want to create a program with more usefull task we first need to learn what are variables. Imagine that I tell you to memorize number 4 and number 1 at the same time. You have just stored two diffrent numbers in your memory. Now if I ask you to add 2 to the first number, you should now have nummber 5 and 2 in your memory. We could also add these two numbers together and obtain the result 7. With this example above I just showed you how computer stores and operates with variables. In almost every programming language variables need a **type** and a **name** that identifies it from the others. We can give variables any kind of name while the start of the name doesn't start with a number or has a space anywhere in the name. C++ has quite a lot of storage types. I will show you the basic ones. +[http://www.braynzarsoft.net/image/100262][Variable types] The most basic ones are int (intagers, 3), char (characters; 'a'), float (floating point; 3.14), double (it's the same as float, except that it has greater range of data) and bool (returns true or false). ##Declaring variables## When we declare variables in C++ we must declare the variable with its type (int, float, char...) before its first use. This informs the compiler the size it needs to store in the memory and how to work with this variable. The syntax for declaring a new variable in C++ is straightforward. int myFirstVariable; float anotherVariable; If we are declearing multiple variables of the same type, they can all be decleared in the same line. All we have to do is seperate them with a commas (,). int x, y, z; The declaration of x, y and z variables has the same meaning as this int x; int y; int z; There is one more thing you need to know about variables. When you decleare your variable, you can set its value right after the name of the variable, or you can do it later in the program. When we set the value of a variable we just decleared, we must add a **equal** (=) sign after the **name** of the variable. int myVariable = 10; float anotherVariable = 3.14; Let me just explain how initialization of variables works. The part on the right side of the variable is processed first, then the result of that is stored in the variable on the left side. As I showed you before, we can declare the variables in the same line. We can also set the value of these variables in the same line, we just need to add a comma between the diffrent variables. int a = 0, b = 1, c = 3; We can also set the value of variables later in the code. int myVariable; float anotherVariable; myVariable = 10; anotherVariable = 3.14; cout<<"Value of myVariable is " << myVariable << "nValue of anotherVariable is " << anotherVariable; Value of myVariable is 10 Value of anotherVariable is 3.14 You can also set the value of a variable equal to another variable or any other kind of mathematical operation (multiplying, addition...). int myVariable = 10; int anotherVariable = 3; int result; result = myVariable + anotherVariable; cout<<result; 13 When we set the value of a variable we can also change that value later in the program, either with cin or by setting the value in the code. int main() { int a = 10, b = 9; cout<<"The value of a is " << a << ", the value of b is " << b << endl; a = 25; cout<<"Enter the value of b: "; cin>>b; cout<<"Now the value of a is " << a << ", and the value of b is " << b; return 0; } The value of a is 10, the value of b is 9 Now the value of a is 25, and the value of b is 13 ##Local and Global variables## In c++ we have two kinds of variables. Local and global. The local variables are declared inside a function and can be accessed only inside the same function. Its lifetime starts with the decleartion of the variable and ends at the end of the function. int main() { int a = 0; cout<<a; return 0; } 0 Global variables are declared outside of the function (usually at the start of the code). Variable that was decleared this way can be accessed from any function in the program, and its value can be changed from anywhere in the program. Global variables aren't used that much, because the program becomes confusing to read. int a = 0; int main() { cout<<a; return 0; } 0 ##Basic Input/Output ## In C++ we have a stream object like cout, that is used for storing data in variables. The name of the stream object is *cin*. Its syntax is similar to cout. First we write cin, after that follows extraction operator (>>), and the name of the variable in which we want to store data. int myAge; cin>>myAge; When we execute our program, when we get to the cin part, the computer will wait for the user to input data. It will wait until you input something on the keyboard and press enter. After the cin is executed, the output after that will be always in the next line. #include <iostream.h> using namespace std; int main() { int age; cout<<"Enter your age: "; cin>>age; cout<<"My age is " << age; return 0; } Enter your age: 25 My age is 25 You can see that creating an input in a program in C++ is straightforward and easy, but this has a big drawback. What if the user inputes something else that our data type doesn't support? In that case the extraction operations fails and the program continues with an unasigned variable. This can produces some problems if the variable is used later. ##Simple calculator## Now that we know how variables work and how to get input for the user, we can create a calculator. The proram will ask us to input values of the numbers and then it will add this two numbers together. int main() { int a, b, result; cout<<"Enter a number: "; cin>>a; cout<<"Enter another number: "; cin>>b; result = a + b; cout<<a << " + " << b << " = " << result; return 0; } Enter a number: 3 Enter another number: 50 3 + 50 = 5 There is one thing we can also do with cout, that I forgot to tell you about in the previous tutorial. Insted of creating variable result in our calculator, we can simply output the sum of variables with cout. cout<<a << " + " << b << " = " << a + b; ##Working with char## When it comes to doing work with characters (char), things might get a little tricky. Char is basicaly an an intager that stores 1 byte intager that is a character. In order to understand how characters work in C++, you must first get to know the ASCII (**A**merican **S**tandard **C**ode for **I**nformation **I**nterchange) table. +[http://www.braynzarsoft.net/image/100266][ASCII Table] Codes from 0 to 31 are unprintable chars, that are used for formatting. Codes from 32 to 127 are printable characters, that represent nubers, letters, operators...