C++: Variables

Author: Ronald S. Holland
at
Total Application Works

ronholland@sumtotalz.com





To contact us



[
HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works


    a file (e.g., using Notepad).)
  • Tell a friend about this site (copy and paste the following HTML page into
  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CPP/CPP_Variables.html">
    Programming Tutorials III </a>
    </table>
    </body>
    </html>


Introduction

This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on c++ or programming. In this tutorial, we will briefly discuss variables, as we ease into c++. c++ as a programming language is widespread and can be found on nearly all computers.

C++ Variables

What is a variable? A variable in programming is a structure that holds data and is uniquely named by the programmer. It holds the data assigned to it until a new value is assigned or the program is finished. A variable is a container in memory that holds data. A variable can be depicted as:

 
  This variable contains the value of ten
             |    |
             |    |
variable     | 10 | 
container -> +----+
 
Figure 1: A variable


The variable name is the memory location where the data is stored. Variables can contain:

  • Integers - which can be expressed in any number, octal, or hexadecimal format.
    • numbers - can be expressed in the form of 10, 10.0, or -2
      • Floating point - Examples are 10.15, -10.15, 1015e-2
    • hexadecimal (base 16) - are characters preceded by a 0x (zero-x) and followed by numbers from 0-9, and/or a-f. The value of forty-four is expressed as "0x2C".
    • octal (base 8) - the value of forty-four is expressed as 054. In this example, you precede the number 54 by the number '0' (zero).

    Integer Long Octal Hexadecimal
    0 0L 0 0x0
    1 1L 01 0x1
    8 8L 001 0x8
    10 10L 012 0xA
    15 15L 017 0XF
    16 16L 020 0x10
    100 100L 0144 0x64
    Figure 2: Examples of integer literals.


  • Boolean - values of true or false.
  • String values - May be enclosed by single ' or double " quotes. The \' or \"sequence of characters will insert a quote character into a string.
  • Arrays - is a container that holds any of the above types.
  • Objects - can be thought of as an instance of a class.
When a variable is declared in c++, it is declared with a data type and variable name. When you define a variable in C++, you must tell the compiler what kind of variable it is: an integer, a character, etc. This information tells the compiler how much room to set aside and what kind of value you want to store in your variable. For example,
    data type anyName - the variable anyName has a data type declared with it.
In the next example,
    int anyName = 10 <-- the variable anyName is declared with the data type of integer.

The first letter in the Variable name is an upper or lower case A through Z or the "_" character. The remaining characters may consist of the same characters or it may contain digits 0 through 9.

Variable declaration examples

Consider the following example:

Definition and Creation Variable int Array float Array double Array
  
    int a = 5 ;

    int x[] ;
    x = new int[5] ;

    float y[] ;
    y = new float[5] ;

    double xx[] ;
    xx = new double[5] ;
  
   
| 5 |
+---+
  
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
Figure 3: Variable and Array Definition and Creation


There are a few points that are being made by the depictions in Figure 1. Each array shown above:

  1. Can only contain one data type;
  2. Are of a fixed length as defined;
  3. Can be referenced by a variable name;
  4. Depict data that is stored in contiguous memory locations;
  5. Depict data that is stored and referenced in sequential order.

Size of various primitive data types

The size of various primitive data types is found in the following table.

Type Size
bits
Type Size
bits
Type Size
bits
Type Size
bits
bool
1
byte
8
int
32
float
32
char
16
short
16
long
64
double
64
Figure 5: Size of variable types.


In Figure 5, a byte is not a data type. It is a measure of size.

 
#include "stdafx.h"

#include <iostream>
#include <cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   cout << "The size of an boolean is:\t\t"    
        << sizeof(bool)    << " bits.\n";
   cout << "The size of a short byte is:\t"  
         << " 1 byte.\n";
   cout << "The size of an int is:\t\t"    
        << sizeof(int)    << " bytes.\n";
   cout << "The size of a float is:\t\t"   << sizeof(float)  
        << " bytes.\n";
   cout << "The size of a char is:\t\t"    << sizeof(char)  
        << " byte.\n";
   cout << "The size of a short int is:\t" << sizeof(short)  
        << " bytes.\n";
   cout << "The size of a long int is:\t"  << sizeof(long)   
        << " bytes.\n";
   cout << "The size of a double is:\t"    << sizeof(double)
        << " bytes.\n";

   return 0;
}
 
 
Output: 
The size of an boolean is:      1 bit
The size of an int is:          4 bytes.
The size of a float is:         4 bytes.
The size of a char is:          1 byte.
The size of a short int is:     2 bytes.
The size of a long int is:      4 bytes.
The size of a double is:        8 bytes.
 
Figure 6: Determining the size of variable types.







HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works