CPP Defining Variables

Defining variables in C++.

To define a variable you start with the type and then the variable name.
Source code Result
// Demonstration of variables
#include <iostream>

int main() {
   using std::cout;
   using std::endl;
   // width and length is both variables of same type
   // but only width is  given a value when defined;
   unsigned short int width = 25, length;
   length = 12;

   // create  an unsigned short and initialize with result
   // of multiplying width by length
   unsigned short int area  = (width * length);

   cout << "Width:" << width << "\n";
   cout << "Length: "  << length << endl;
   cout << "Area: " << area << endl;
   return 0;
}
Width:25
Length: 12
Area: 300

C++ is case sensitive. In other words, uppercase and lowercase letters are considered to be different when you a naming a variable.

You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.