One man’s constant is another man’s variable.
Alan J. Perlis
A variable is a named space in the computer’s memory that you can write values into or read values from. For example, if you have an integer variable “foo”, you can perform the following operations:
- Write value 10 into variable “foo”
- Read value from variable “foo”, the result will be 10
- Write value 20 into variable “foo”
- Read value from variable “foo”, the result will be 20
In C/C++ each variable has a name and a type. The variable name must start from a letter and contain only letters, digits and underscore. The variable name must be unique (in the current “scope” – more about this later). The variable type determines how much memory computer needs to allocate for this variable and the kind of data we can store in it. You must always declare the variable (announce its name and type) before you can use it to make sure computer knows what to do:
int foo; // declare integer variable foo
foo = 10; // write value 10 into variable foo
cout << foo << endl; // read value from variable foo and
// print it on the screen
foo = 20; // write value 20 into
cout << foo << endl; // read value from variable foo and
// print it on the screen
The program fragment above uses comments (start from //) and special variable “cout” from our first lesson. The program prints the two numbers on the screen:
10 20
Create a new project in Visual Studio and try it out! Note that each statement ends with a semicolon ‘;’ that indicates to C/C++ compiler the boundaries of the expression. Don’t forget about them!
Most of the programs process data entered by user. You can read the variable value from the user using another special variable “cin” (full name std::cin) and a special operator >> (note that the input operator >> has a different direction from the output operator <<):
int foo;
cout << “Enter a number: “; // no << endl because we want user to
// enter a number on the same line
cin >> foo; // read the user input into variable foo
cout << “You’ve entered “ << foo << endl; // print it back
This program prompts the user to enter a number, reads it from the user and then prints it back to screen.
The C/C++ defines the usual set of mathematical and logical operators: +, -, *, /, % (reminder from dividing one integer by another), < (less), > (greater), <= (less or equal), >= (greater or equal), == (equal) that can be used to calculate variable values. The usual order of operations apply and we use parentheses ‘(‘ and ‘)’ to force the order of operations we need:
int foo; int bar; foo = 10; bar = (foo + 5) * 10 + 1; cout << bar << endl; // this would print 51
Before reading a value from a variable, it is important to always write a value into this variable first. If you try to read the value from an uninitialized variable (i.e. a variable you didn’t write before) then you will might get an error and your program might not behave as expected. This is one of the most common programming errors:
int foo; cout << foo << endl; // this is an error, foo is un-initialized
You can combine a variable declaration with the assignment of the initial value to make sure you have no un-initialized variables:
int foo = 10; int bar = foo + 5; cout << foo << endl; // prints 10 cout << bar << endl; // prints 15
There many variable types available in C/C++ languages, the most commonly used are:
| Type | Values | Range in Visual Studio |
| bool | logical, true (1) or false (0) | true (1) or false (0) |
| char | characters (e.g. ‘a’) | ASCII (0 to 255) |
| int | the integer numbers | –2,147,483,648 to 2,147,483,647 |
| unsigned int (or simply unsigned) | the integer numbers greater or equal to 0 | 0 to 4,294,967,295 |
| float | the floating point numbers | 3.4E +/- 38 (7 digits) |
| double | the floating point numbers with range larger than or equal to the range of type float | 1.7E +/- 308 (15 digits) |
Using special operator sizeof() you can find out how much memory will be taken by a variable of a given type. Try the following code in Visual Studio to find out how much memory is used by a char type:
cout << “Char type takes “ << sizeof(char) << “ bytes” << endl;
Exercises
1) Write a program that prints how much memory is taken by each of the types from the table above using the sizeof() operator.
2) Define 2 integer variables a, b; read their values from the user and print out the average of these two values.
3) Write a program that exchanges the values of two variables a, b.
4) (Hard) Write a program that exchanges the values of two variables a, b without using an extra variable.
Pingback: Practical programming « Random Notes