|
|
![]() |
![]() |
![]() |
C++ Reference: Variables & Constants
Fundamental Types
| Category | Type | Range | Size (bytes) * |
| Character | char | [-128, 127] | 1 byte |
| signed char | [-128, 127] | 1 byte | |
| unsigned char | [0,255] | 1 byte | |
| wchar_t | [0, 65535] | 2 bytes | |
| Integer | |||
| short | [-32768, 32767] | 2 bytes | |
| short int | [-32768, 32767] | 2 bytes | |
| signed short int | [-32768, 32767] | 2 bytes | |
| signed | [-2147483648, 2147483647] | 4 bytes | |
| signed int | [-2147483648, 2147483647] | 4 bytes | |
| int | [-2147483648, 2147483647] | 4 bytes | |
| long | [-2147483648, 2147483647] | 4 bytes | |
| long int | [-2147483648, 2147483647] | 4 bytes | |
| signed long int | [-2147483648, 2147483647] | 4 bytes | |
| long long | [-9223372036854775808, 9223372036854775807] | 8 bytes | |
| unsigned short | [0, 65535] | 2 bytes | |
| unsigned short int | [0, 65535] | 2 bytes | |
| unsigned | [0, 4294967295] | 4 bytes | |
| unsigned int | [0, 4294967295] | 4 bytes | |
| unsigned long | [0, 4294967295] | 4 bytes | |
| unsigned long int | [0, 4294967295] | 4 bytes | |
| unsigned long long | [0, 18446744073709551615] | 8 bytes | |
| Floating Point | |||
| float | [-3.4*10^38, 3.4*10^38] (7 decimal place accuracy) | 4 bytes | |
| double | [-1.7*10^308, 1.7*10^308] (15 decimal place accuracy) | 8 bytes | |
| long double | [-1.7*10^308, 1.7*10^308] (15 decimal place accuracy) | 8 bytes | |
| Boolean | bool | {true, false} | 1 byte |
| Void | void | | |
Note: The sizes above are Visual C++-specific. Other compilers may vary, but the relationships in the Guaranteed Size Relationships section always hold.
Guaranteed Size Relationships
| 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) | |
| 1 <= sizeof(bool) <= sizeof(long) | |
| sizeof(char) <= sizeof(wchar_t) <= sizeof(long) | |
| sizeof(float) <= sizeof(double) <= sizeof(long double) | |
| sizeof(N) = sizeof(signed N) = sizeof(unsigned N) | |
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 stringit'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;
![]() |
![]() |
![]() |
| Home | | | Reference | | | Play Games! | | | Forum | | | Site Map | | | Contact Us |






