一、x&y 按位與運算

x|y 按位或運算，相同為0，不同為1

int Vac=3；
int main(){
int Vac=10;
::Vac ;
count<<::Vac;
count<<Vac;
return 0;
}

int i=3,j=4;
i?i : j;

int i=1,j=2;
int k=i j;

x=x 1;

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;}

int a=5,b=3;
!a&&b ;

char foo(void)
{
unsigned int a=6;
int b=-20;
char c;
(a b>6)?c=1:c=0;
return c;
}

#define SQR(x) (x*x)
main(){
int a,b=3;
a=SQR(b 2);
}

#define SECONDS_PRE_YEAR(60*60*24*365)UL

#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