The summary of C++
The Size of the Primitive Data
Type | Size | Range |
---|---|---|
void | 0 | N/A |
bool | 1 byte | N/A |
short | 2 bytes | -0x8000~0x7FFF |
unsigned short | 2 bytes | 0~0xFFFF |
int | 4 bytes | -0x80000000~0x7FFFFFFF |
unsigned int | 4 bytes | 0xFFFFFFFF |
long | 4 bytes | -0x80000000~0x7FFFFFFF |
unsigned long | 4 bytes | 0~0xFFFFFFFF |
long long | 8 bytes | 0~0xFFFFFFFFFFFFFFFF |
char | 1 byte | -0x80~0x7F |
unsigned char | 1 byte | 0~0xFF |
wchar_t | 2 bytes | 0~0xFFFF |
float | 4 bytes | 3.4E-38~3.4E+38 |
double | 8 bytes | 1.7E-308~1.7E+308 |
long double | 8 bytes |
* 以上sizeof通过Windows XP 32位平台测试,其中某些类型数据的字节数和数值范围由操作系统和编译平台决定。比如16位机上,sizeof(int) = 2,而32位机上sizeof(int) = 4;32位机上sizeof(long) = 4,而64位机上sizeof(long) = 8。除此之外,注意64位机上的pointer占8byte
Numeric Type Data Conversion
Priority:
long double > double > float > unsigned long > long > unsigned int > int
Syntax:
static_cast<type>(value) or (type)value
The principle of Function call
Each time a function is invoked, the system stores its arguments and variables in an area of memory, known as stack, which stores elements in last-in-first-out fashion. When a function calls another function , the caller’s stack space is kept intact, and new space is created to handle the new function call. When a function finishes its work and returns to its caller, its associated space is released.
Passing Parameters by Values and by References
Passing Parameters by Values:
void increment(int n)
Passing Parameters by References:
void swap(int &n1, int &n2)
Static Local Variables
After a function completes its executions, all its local variables are destroyed. Sometimes, it is desirable to retain the value stored in local variables so that they can be used in the next call. C++ allow us to use static local variables to do this.
static int x = 1;