1. 基本类型变量与零值比较
基本类型与零值比较
int main()
{
//bool类型
bool flag;
cin >> flag;
if(flag)
cout << "bool !=" << endl;
else
cout << "bool ==" << endl;
//指针类型
bool* flag2 = &flag;
if(NULL != flag2)
cout << "point !=" << endl;
else
cout << "point ==" << endl;
//float类型
/*float f = 0.0;
if (0.0 == f)
cout << "f==0.0" << endl;
else
cout << "f!=0.0" << endl;*/
/*float f1, f2, f3, f4;
f1 = 169.02;
f2 = 169;
f3 = f1 - f2;
f4 = 0.02;
if (f3 > f4)
cout << "f3 > f4" << endl;
else if (f3 < f4)
cout << "f3 < f4" << endl;
else
cout << "f3 == f4" << endl;*/
return 0;
}
// 实数型是有精度的,存的数不一定是计算所得;运算符“==”时,要判断的尽量放右侧,防止使用运算符“=”
2. sizeof 和 strlen 的区别
2.1 sizeof 是操作符,参数可以是数组、指针、函数、类对象、联合体等;strlen是库函数,参数必须是以 '\0' 结尾的字符串,参数为 (char*) 类型
2.2 sizeof 在编译时就得到sizeof的结果,strlen函数在运行时才能得到结果
2.3 sizeof 得到的是占内存的大小,能容纳参数的最大内存,字符串数组的 '\0' 是 1 ;strlen 得到的是字符串的实际长度,遇到 '\0' 就停止且不包括 '\0'
2.4 sizeof 的参数是数组时,数组不退化;strlen 的参数是数组时,退化为指针
2.3
class MyClass
{
public:
private:
int a;
char c;
double d;
};
int main()
{
/*float f = 0.0;
if (0.0 == f)
cout << "f==0.0" << endl;
else
cout << "f!=0.0" << endl;*/
cout << "sizeof(class): " << sizeof(MyClass) << endl << endl;
cout << "sizeof(string): " << sizeof(string) << endl << endl;
string s = "china";
cout << "sizeof(string object): " << sizeof(s) << endl;
cout << "length(string object): " << s.length() << endl << endl;
union MyUnion
{
int age;
char c;
//string s; //output: 32
//sizeof(string)在32位系统中占28字节,字节对齐后为 32 字节,因此sizeof(MuUnion)输出32字节
double d;
};
cout << "siezof(union): " << sizeof(MyUnion) << endl << endl ;
//注意转义字符,只转义最邻近的
cout << "1st part: " << endl;
char arr1[] = "china";
cout << "strlen(): " << strlen(arr1) << endl;
cout << "sizeof(): " << sizeof(arr1) << endl << endl;
cout << "2nd part: " << endl;
char arr2[] = "china\0"; //arr2[]="china "
cout << "strlen(): " << strlen(arr2) << endl;
cout << "sizeof(): " << sizeof(arr2) << endl << endl;
cout << "3rd part: " << endl;
char arr3[] = "chin\0a"; //arr3[]="chin"
cout << "strlen(): " << strlen(arr3) << endl;
cout << "sizeof(): " << sizeof(arr3) << endl << endl;
cout << "4th part: " << endl;
char arr4[] = "china\\0"; //arr4[]="china\0",只转义最邻近的
cout << "strlen(): " << strlen(arr4) << endl;
cout << "sizeof(): " << sizeof(arr4) << endl << endl;
cout << "5th part: " << endl;
char arr5[] = "china\\\0"; //arr5[]="china\ "
cout << "strlen(): " << strlen(arr5) << endl;
cout << "sizeof(): " << sizeof(arr5) << endl << endl;
cout << "6th part: " << endl;
char arr6[] = "chin\\0a"; //arr6[]="chin\0a"
cout << "strlen(): " << strlen(arr6) << endl;
cout << "sizeof(): " << sizeof(arr6) << endl << endl;
cout << "7th part: " << endl;
char arr7[] = "chin\\\0a"; //arr7[]="chin\0a"
cout << "strlen(): " << strlen(arr7) << endl;
cout << "sizeof(): " << sizeof(arr7) << endl << endl;
return 0;
}
/*output
sizeof(class): 16
sizeof(string): 28
sizeof(string object): 28
length(string object): 5
siezof(union): 8
1st part:
strlen(): 5
sizeof(): 6
2nd part:
strlen(): 5
sizeof(): 7
3rd part:
strlen(): 4
sizeof(): 7
4th part:
strlen(): 7
sizeof(): 8
5th part:
strlen(): 6
sizeof(): 8
6th part:
strlen(): 7
sizeof(): 8
7th part:
strlen(): 5
sizeof(): 8
*/
3. 关键字 static 在 C 语言的和 C++ 中的区别
参考:
在 C 中使用 static 来修饰局部静态变量、外部静态变量/函数;
在 C++ 中,不仅包括 C 的功能,还支持定义类的成员变量(静态成员变量)和函数(静态成员函数),C++ 的静态成员可以在对象实例中共享、传递信息
{
A. static 变量生存期为整个程序运行期间;
B. static 变量在某一函数/全局中只定义初始化一次;
C. static 局部静态变量和全局静态变量可以同名;
D. static 局部静态变量和全局静态变量若不初始化,则默认是 0 或空字符;
E. static 变量存储在栈上;
F. static 变量在函数体外定义时,在整个程序中可以调用;在函数体内定义时,则只能在只能在函数体内有效,呼应 B ;
}
{
C++中,类的静态成员变量不能在类内定义,只能在类内声明,且定义时不能标示 static;
}
static 变量具有记忆性和全局性的特点: