1. Technology

Declaring Variables

Hello World


This is the second tutorial in the series Online C++ Tutorial for beginners and is about scalar variables. If you want to start at the beginning, begin with the first Online C++ Tutorial.

This is a rewriting of the original C++ tutorials into single page bite size tutorials which can usually be run on either codepad.org or ideone.com.

It will eventually include C++ 11, though you may need to install a compiler on your computer, but that won't be for a few months.

Note these online services and others are provided free by third party providers so please don't abuse them. I've used them because they save you the hassle of installing a C++ compiler. You might consider doing that and running them instead because with many users hitting these services the response may get slow.

In this tutorial we'll look at variables in C++. These are places to hold values which are usually of these types:

  • int - this holds an integer - usually numbers from -2 billion to 2 billion (-2147483647 to +2147483647). It's also possibleto have unsigned ints which go from 0 to 4 billion.
  • float - this holds a single precision floating point number between 1.175494351 E – 38 and 3.402823466 E + 38
  • double - this holds a double precision floating point number in the range 2.2250738585072014 E – 308 to 1.7976931348623158 E + 308
  • std::string - this is a string in C++ holding text values like "David".
  • enums - this is type which a range of named values- each corresponding to a certain int value and we'll look at these in a later tutorial.

In this tutorial we'll stick to just int and float variables. Enums will take a tutorial of their own and strings will take a few. A double is like a float but holds larger numbers with more decimal places of precision (floats are only good for 6 or 7 digits of precision, doubles up to 15). So feel free to use double instead of float.

Declaring Variables

Before you can use a variable to hold numbers in C++, you must declare them. A declaration is very simple in the format type name where type is int, float etc and name is the name of the variable. Multiple variables can be declared on the same line and single variables can be assigned a value. Here are some examples:

int a,b;
int c;
int d =42;

This declares four variables, all of type int. They are called a,b,c and d.

Where can I declare variables?

There are several places you can declare them. The main places are:

  1. Globally (outside of functions and object and class methods)
  2. Anywhere in a block of code in a function
  3. In a class or struct (these are called member variables).
  4. In some statements like for. For instance for (int i=0;i++i;i<10) {...

The example below shows them used globally and in a function. I'll also show them later when we come onto function. You can see this example on ideone.com with Example 1.

// ex2.cpp : Defines the entry point for the console application.

#include <iostream>

int total(int value1,int value2) {
    int totalvalue = value1 + value2;
    return totalvalue;

int main(int argc, char* argv[])
    float a = 4.5;
    int d= 42;
    int e = total(6,7) ;
    std::cout << "a = " << a <<std::endl;
    std::cout << "d = " << d << " e = " << e << std::endl;
    return 0;

The output from this is:

a = 4.5
d = 42 e = 13

Using a Variable

After a variable is declared, it can be used by any code that occurs after the declaration. The variable is said to be in scope. The three three declarations after main declare a, d and e and initialize them with an value.

The value for e though comes from calling a function total() which takes two int values value1 and value2, adds them together in a variable totalvalue then returns that.

The two lines std::cout just output the values of a, d and e.

The in scopeness of a variable is important. The easiest way is to remember that in most cases, the scope of a variable is the remainder of the block that it is declared in, even if declared globally. For instance, the variable i is declared twice here:

for (int i=0;i<10;i++) {

int k= 8;

for (int i=0;i<10;i++) {

The scope of each i is just within the for loop where it was declared. Note the variable k. It could be used inside the second for loop (it isn't), but not the first because it was declared after it.

If you know a bit of C or C++ then you'll realize that the function total() is a bit contrived. The call could be simply replaced by int e = 6 + 7 but it gives a flavor of creating a function and declaring a variable in it.

You can remove all the std:: prefixes if you add this line after the #include.

using namespace std;

This tells the compiler to check the std namespace for symbols it doesn't recognize sucxh as cout and endl. AFter adding this line, the first cout line looks like this:

cout << "a = " << a <

In the next tutorial I'll look at enums.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C++ Programming
  5. Learn C++ Programming
  6. Online C++ Programming Tutorial for Beginners - Lesson Two - Variables

©2014 About.com. All rights reserved.