Names for Variables
C++ encourages you to use meaningful names for variables.If a variable represents the cost of a trip,you should call it cost_of_trip or costOfTrip ,not just x or cot .You do have to follow a few simple C++ naming rules:
- The only characters you can use in names are alphabetic characters,numeric digits, and the underscore (_) character.
- The first character in a name cannot be a numeric digit.
- Uppercase characters are considered distinct from lowercase characters.
- You can’t use a C++ keyword for a name.
- Names beginning with two underscore characters(__hehe) or with an underscore character followed by an uppercase letter(_Hehe) are reserved for use by the implementation—that is,
the compiler and the resources it uses. Names beginning with a single underscore character are reserved for use as global identifiers by the implementation. - C++ places no limits on the length of a name,and all characters in a name are significant.However,some platforms might have their own length limits.
C++ Differs from ASCI C(C99):
ANSI C (C99) guarantees only that the first 63 characters in a name are significant.(In ANSI C,two names that have the same first 63 characters are considered identical,even if the 64th characters differ.)
有时变量名前会加上前缀用来指明变量的数据类型,如 n 表示 int,p 表示指针等等。
C++’s Build-in integer Data types
The differences between implementations for type widths can cause problems when you move a C++ program from one environment to another,including using a different compiler on the same system.
- The <climits >file,which represents system limits for various integer types
- sizeof operator returns the data size in bytes.
#include <iostream>
#include <climits>
/* C++'s Build-in integer types: unsigned long , long , unsigned int , int , unsigned short , short , char , unsigned char , signed char , bool
*/
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout<< "int is "<<sizeof(int)<<" bytes."<<endl;
cout<< "short is "<<sizeof n_short <<" bytes."<<endl;
cout<< "long is "<<sizeof n_long <<" bytes."<<endl;
cout<< "long long is "<< sizeof n_llong <<" bytes."<<endl;
cout<<endl;
cout<< "Maximum values:"<<endl;
cout<< "int: "<<n_int<<endl;
cout<< "short: "<<n_short<<endl;
cout<< "long: "<<n_long<<endl;
cout<< "long long: "<<n_llong<<endl<<endl;
cout<< "Minimun int value = "<<INT_MIN<<endl;
cout<< "Bits per byte = "<< CHAR_BIT<<endl;
return 0;
}
New C++11 Types: char16_t and char32_t
单个字符也可以占据不同长度
char16_t ch1 = u'q'; // basic character in 16-bit form
char32_t ch2 = U'\U0000222B'; // universal character name in 32-bit form