Variables & Constants in C++

Variables


Example 1

// Assignment Initialization
double dX = 10.0;

Example 2

// Constructor initialization notation
double dX(2.1);

Example 3

// Copy constructor initialization notation
int iX = 5.0;
int iCopy(iX);

Example 4

// Initialization list initialization
bool bIsDone = {true};

Example 5

// Built-in default constructor initialization-value uncertain
double dX;

Example 6

// Multiple variable list declaration, initializes with
// the default constructor
double dX, dY, dZ;

Example 7

// Assignment operator
double dY = 9;
double dX = dY;

Example 8

// Automatic type conversion, performed by the compiler
int iInteger = 10;
double dX = iInteger;

Example 9

// Type conversion from 'double' to 'int'—truncates at the decimal point.
int iInteger = (int)dDouble;

Example 10

// A void pointer can be used to point to any type of data
int iX = 10;
void* vpVoidPtr = &iX;

Example 11

// Get the size of a data instance in bytes
double dX = 10.0;
int iSize = sizeof(dX);

Example 12

// Get the size of a data type in bytes
int iSize = sizeof(double);

Constants


Example 1

// An old-style ASCII string—it's better to use an array.
const char* kcpName = "XoaX.net";

Example 2

// An ASCII character array with compiler specified length
const char kcaName[] = "XoaX.net";

Example 3

// An ASCII character array initialized with a list. '\0' defines null
const char kcaName[] = { 'X', 'o', 'a', 'X', '.', 'n', 'e', 't', '\0'};

Example 4

// A wide character array: using the prefix 'L'
const wchar_t kwaName[] = L"XoaX.net";

Example 5

// Initialize a single ASCII character using single quotes
const char kcChar = 'X';

Example 6

// Initialize a single, wide character using the prefix 'L'
// and single quotes
const wchar_t kwWideChar = L'X';

Example 7

// Define a float constant using the suffix 'f' or 'F'
const float kfFloat = 3.14159f;

Example 8

// Define a double constant-no suffix needed
const double kdDouble = 3.14159;

Example 9

// Define a long double constant using the suffix 'l' or 'L'
const long double kdLongDouble = 3.14159L;

Example 10

// Define a floating point constant using scientific notation
// (electron charge in coulombs)
const double kdElectronCharge = 1.6e-19;

Example 11

// Define an octal integer constant using a the prefix '0'
//(016 is 14 in decimal)
const int kiOctal = 016;

Example 12

// Define a decimal integer constant-no prefix or suffix required
const int kiDecimal = 63;

Example 13

// Define a hexadecimal integer constant using the prefix '0x'
// (0x1a is 26 in decimal)
const int kiHexadecimal = 0x1a;

Example 14

// Define a pointer to a constant variable
const int* kipPtrToConst;
kipPtrToConst = &iX;

Example 15

// Define a constant pointer to a variable
int* const ipkConstPtr = &iX;
*ipkConstPtr = 5;

Example 16

// Define a constant pointer to a constant variable
const int* const kipkConstPtr = &iX;

Storage Class Specifiers


Example 1

// Use 'register' to suggest storing a variable
// in a register to faster access
register unsigned int uiX = 5;

Example 2

// Use 'auto' to suggest local stack variable storage
// (same as no specification)
auto int iX = 5;
int iY = 10;

Example 3

// Use 'static' to restrict a variable to
// local linkage to its source file
static int iX = 23;

Example 4

// Use 'extern' to link to a variable
// that is defined in a separate source file

// def_file.cpp — separate source file which is
// linked to main.cpp below
int iX = 94;

// main.cpp
#include <iostream>

extern int iX;

int main() {
    std::cout << iX << std::endl;
    return 0;
}

Example 5

// Use 'mutable' to declare variables that are never constant
class CMyClass
{
    // const member functions can change mutable members
    void SetX(int iNewX) const {
        iX = iNewX;
    }
    mutable int iX;
};

Example 6

// Use 'volatile' to tell the compiler that this variable
// should not be optimized since its value may change irregularly,
// (e.g. via some extern device).
volatile int iX;