一、x&y 按位与运算
如x=3 y=3
则:x=y=0011
结果还是:3
x|y 按位或运算,相同为0,不同为1
二、
int Vac=3;
int main(){
int Vac=10;
::Vac++;
count<<::Vac;
count<<Vac;
return 0;
}
结果:4,10
三、
int i=3,j=4;
i?i++:++j;
结果:4 4
五、
int i=1,j=2;
int k=i+++j;
结果:k=3;
六、
x=x+1;
读两次x的地址,编译器不认为左右的x地址相同
x+=1;
x的地址制度一次
x++;
效率由低到高排列
七、
#define product(x) (x*x)
int main(){
int i=3,j,k;
j=product(i++);
//i=5
k=product(++i);
return 0;}
结果;j=9;k=49;
八、
int a=5,b=3;
!a&&b++;
结果:a=5 b=3 因为当&&前面的运算为flase时候,不计算后面的,直接false
九、
char foo(void)
{
unsigned int a=6;
int b=-20;
char c;
(a+b>6)?c=1:c=0;
return c;
}
结果:c=1;
无符号和有符号的相加,有符号的会自动转化为无符号类型,即b会转化成一个很大的整数,所以肯定大于6;
十、
#define SQR(x) (x*x)
main(){
int a,b=3;
a=SQR(b+2);
}
结果:a=11;
应为传过去的是b+2
而实际执行的是b+2*b+2;
而不是(b+2)*(b+2)
如果SQR(x) ((x)*(x)) 则结果就是25了。
十一、
预处理,一年中有多少秒,应为牵扯的会溢出,所以数据类型要注意
#define SECONDS_PRE_YEAR(60*60*24*365)UL
十二、
宏定义:MIN
#define MIN(A,B) ((A)<=(B)?(A):(B))注意括号的使用。
十三、
/************************************************************
程序员面试宝典6.3_1
************************************************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct {
short a1;
short a2;
short a3;
} A;
struct {
long a1;
short a2;
} B;
int main()
{
char *ss1 = "0123456789";
char ss2[] = "0123456789";
char ss3[100] = "0123456789";
int ss4[100];
char q1[] = "abc";
char q2[] = "a/n";
char *q3 = "a/n";
char *str1 = (char *) malloc(100);
void * str2 = (void *) malloc(100);
cout <<sizeof(ss1)<<" ";
cout <<sizeof(ss2)<<" ";
cout <<sizeof(ss3)<<" ";
cout <<sizeof(ss4)<<" ";
cout <<sizeof(q1)<<" ";
cout <<sizeof(q2)<<" ";
cout <<sizeof(q3)<<" ";
cout <<sizeof(str1)<<" ";
cout <<sizeof(str2)<<" ";
cout <<sizeof(A)<<" ";
cout <<sizeof(B)<<" ";
return 0;
}
结果:
4 11 100 400 4 3 4 4 4 6 8
/************************************************************
程序员面试宝典6.3_2
************************************************************/
#include <iostream>
#include <stdio.h>
using namespace std;
class A {
};
class A2 {
char d,e;
};
class B {
};
class C {
char b,c;
};
struct D {
int x,y;
};
int main()
{
cout<<sizeof(A)<<endl;
cout<<sizeof(A2)<<endl;
A *p1 = new A();
A p2;
A *p3;
cout<<sizeof(p1)<<endl;
cout<<sizeof(p2)<<endl;
cout<<sizeof(p3)<<endl;
cout<<sizeof(B)<<endl;
cout<<sizeof(C)<<endl;
cout<<sizeof(D)<<endl;
return 0;
}
output:
1
2
4
1
4
1
2
8
/************************************************************
程序员面试宝典6.3_3
************************************************************/
#include <iostream>
#include <stdio.h>
using namespace std;
class A1 {
public:
int a;
static int b;
A1();
~A1();
};
class A2 {
public:
int a;
char c;
A2();
~A2();
};
class A3 {
public:
float a;
char c;
A3();
~A3();
};
class A4 {
public:
float a;
int b;
char c;
A4();
~A4();
};
class A5 {
public:
double d;
float a;
int b;
char c;
A5();
~A5();
};
int main()
{
cout<<sizeof(A1)<<endl;
cout<<sizeof(A2)<<endl;
cout<<sizeof(A3)<<endl;
cout<<sizeof(A4)<<endl;
cout<<sizeof(A5)<<endl;
return 0;
}
output:
4
8
8
12
24
/************************************************************
程序员面试宝典6.3_7
************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
string strArr1[] = {"Trend","Micro","Soft"};
string *pStrArr1=new string[2];
pStrArr1[0]="US";
pStrArr1[0]="CN";
cout<<sizeof(strArr1)<<endl;
cout<<sizeof(string)<<endl;
cout<<sizeof(pStrArr1)<<endl;
for (int i=0;i<sizeof (strArr1) /sizeof(string);i++)
cout<<strArr1[i];
cout<<endl;
for (int j=0;j<sizeof (pStrArr1) *2 /sizeof(string);j++)
cout<<pStrArr1[j];
return 0;
}
output:
48
16
4
TrendMicroSoft
/************************************************************
程序员面试宝典6.3_9
************************************************************/
#include <iostream>
#include <string>
using namespace std;
int test(char vax[])
{
return sizeof(vax);
}
int main ()
{
char var[10];
cout << test(var)<<endl;
return 0;
}
output:
4
/************************************************************
程序员面试宝典6.3_11
************************************************************/
#include <iostream>
#include <string>
using namespace std;
class A
{
};
class A2
{
};
class B: public A
{
};
class C:public virtual B
{
};
class D:public A, public A2
{
};
int main(int argc, char *argv[])
{
cout << "sizeof(A):" << sizeof(A) << endl;
cout << "sizeof(B):" << sizeof(B) << endl;
cout << "sizeof(C):" << sizeof(C) << endl;
cout << "sizeof(D):" << sizeof(D) << endl;
return 0;
}
output:
sizeof(A):1
sizeof(B):1
sizeof(C):4
sizeof(D):1
待续……