#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int ret = 0 ;
int i = 0 ;
for ( i = 0 ; i < 2 ; i++ )
{
fork();
printf ( "a\n" );
}
return ret;
}
#include <stdio.h>
#include <iostream>
using namespace std ;
class A
{
public :
int &b;
int a;
};
int main()
{
int ret = 0 ;
cout << sizeof (int *) << " " << sizeof (A) << " " << sizeof (int ) << endl;
unsigned int a = 0x1FFFFFF7 ;
unsigned char b = a;
char c = a;
char *p = ( char * )&a;
long long data = 0xf1f2f3f4f8f0f5f7 ;
cout << sizeof (long long ) << endl;
printf ( "%x, %x, %x, %x\n" , b, c, *p, data );
long long a1 = 1 , b1 = 2 , c1 = 3 ;
printf ( "%d, %d, %d\n" , a1, b1, c1 );
return ret;
}
1. #define 定义的宏只在本文件有效
#ifndef MYFILE
#define MYFILE
void test();
#endif
#include <iostream>
using namespace std;
#define MAX 10
void func()
{
#ifdef MYFILE
cout << "MYFILE is defined" << endl;
#else
cout << "MYFILE is not defined" << endl;
#endif
return ;
}
#include "myheader.h"
#include <iostream>
using namespace std;
extern void func();
int main()
{
func();
return 0 ;
}
g++ use1.cpp use2.cpp -o use
./use
MYFILE is not defined
故下面代码是为了防止同一个文件包含一个头文件多次
#ifndef MYFILE
#define MYFILE
#endif
===========================
#include <iostream>
using namespace std;
int cnt = 0 ;
int show()
{
cnt = 9 ;
return 0 ;
}
int main()
{
cout << show() << ', ' << cnt << endl;
return 0 ;
}
#include "myheader.h"
#include <iostream>
using namespace std ;
extern void func();
extern const int v1;
extern const int v2;
class Test
{
private :
static int a;
};
int Test::a = 10 ;
void f( const int &a )
{
cout << "test f(const int&)" << endl;
return ;
}
int mal( int a, int b = 2 , int c = 4 )
{
cout << a + b + c << endl;
return 0 ;
}
template <typename T> T add( T x, T y )
{
return x + y;
}
class A
{
public :
int a;
int &b;
};
void prim( int m, int n )
{
if ( m >= n )
{
while ( m % n != 0 )
{
++n;
}
cout << n << endl;
m /= n;
prim( m, n );
}
return ;
}
typedef int * PINT;
typedef int *PINT1;
typedef int fun( int *, int );
int testfun( int *p, int a )
{
return a;
}
class ConstRef
{
public :
ConstRef( int ii ) : i(ii), ci(i), ri(ii){}
void show()
{
cout << ci << ' ' << ri << endl;
ri = 8 ;
cout << ci << ' ' << ri << endl;
}
private :
int i;
const int ci;
int &ri;
};
int main()
{
int hah = 0 ;
int &p33 = hah;
cout << sizeof (int ) << ' ' << sizeof (const int ) << ' ' << sizeof (p33) << endl;
cout << sizeof (ConstRef) << endl;
ConstRef obj(3 );
obj.show();
int t1 = 9 ;
const PINT p = &t1;
const int *p2 = &t1;
const PINT1 p3 = &t1;
*p3 = 3 ;
*p = 10 ;
cout << t1 << endl;
int *p1;
int data = 27 ;
while ( cin >> data )
{
prim( data, 2 );
cout << "===============================" << endl;
}
cout << "sizeof(A)=" << sizeof (A) << endl;
cout << "sizeof(int) = " << sizeof (int ) << endl;
add<float >( 4.3 , 3 );
func();
cout << v2 << endl;
const char *pp;
cout << mal( 2 ) << endl;
f(1 );
return 0 ;
}
#include <iostream>
using namespace std ;
#define MAX 10
const int v1 = 10 ;
extern const int v2 = 20 ;
void func()
{
#ifdef MYFILE
cout << "MYFILE is defined" << endl;
#else
cout << "MYFILE is not defined" << endl;
#endif
return ;
}