CPP Variables and Constants

Working with Variables in C++

  • A variable is a location in your computer's memory in which you can store a value and from which you can later retrieve that value.
  • The RAM (Random access memory):
  • When you define a variable in C++, you must tell the compiler what kind of variable it is (this is usually referred to as the variable's "type".
  • The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++.
  • Summary of the basic fundamental primitive datatypes in C++ :
    Name Description Size* Range*
    char Character or small integer. 1byte signed: -128 to 127
    unsigned: 0 to 255
    short int
    (short)
    Short Integer. 2bytes signed: -32768 to 32767
    unsigned: 0 to 65535
    int Integer. 4bytes signed: -2147483648 to 2147483647
    unsigned: 0 to 4294967295
    long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
    unsigned: 0 to 4294967295
    bool Boolean value. It can take one of two values: true or false. 1byte true or false
    float Floating point number. 4bytes 3.4e +/- 38 (7 digits)
    double Double precision floating point number. 8bytes 1.7e +/- 308 (15 digits)
    long double Long double precision floating point number. 8bytes 1.7e +/- 308 (15 digits)
    wchar_t Wide character. 2bytes 1 wide character

    * The values of the columns Size and Range depend on the architecture of the system where the program is compiled and executed. The values shown above are those found on most 32bit systems
  • To find how much a data type will occupy you can use the function sizeOf.
    Examples of using sizeOf:
    Source code Result
    #include <iostream>
    
    int main() {
      using std::cout;
      cout << "The size of a bool is:\t\t"
        << sizeof(bool)   << " bytes.\n";
      cout << "The size of a char is:\t\t"
        << sizeof(char)   << " bytes.\n";
      cout << "The size of a short int is:\t"
        << sizeof(short)  << " bytes.\n";
      cout << "The size of an int is:\t\t"
        << sizeof(int)    << " bytes.\n";
      cout << "The size of a long int is:\t"
        << sizeof(long)   << " bytes.\n";
      cout << "The size of a float is:\t\t"
        << sizeof(float)  << " bytes.\n";
      cout << "The size of a double is:\t"
        << sizeof(double) << " bytes.\n";
      return 0;
    }
    The size of a bool is:	 1 bytes.
    The size of a char is:	 1 bytes.
    The size of a short int is: 2 bytes.
    The size of an int is:      4 bytes.
    The size of a long int is:  4 bytes.
    The size of a float is:	 4 bytes.
    The size of a double is:    8 bytes.
    

    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.