C++学习整理

打印字符串

#include <iostream>
extern int pow(int, int);
int main()
{
	int val = 2;
	int exp = 15;
	cout << "The Powers of 2\n";
	for ( int cnt=0; cnt <= exp; ++cnt )
	cout << cnt << ": "
	<< pow(val, cnt) << endl;
	return 0;
}

内联函数inline,最好放在头文件中

inline int min( int v1, int v2 ) { /* ... */ }

链接指示符 extern “c”,告诉编译器函数编写语言类型

// 单一语句形式的链接指示符
extern "C" void exit(int);
// 复合语句形式的链接指示符
extern "C" {
int printf( const char* ... );
int scanf( const char* ... );
}
// 复合语句形式的链接指示符
extern "C" {
	#include <cmath>
}

链接指示符不能出现在函数体中,并且如果需要c调用c++程序也需要在前面生命链接指示符,链接指示符如果声明多次,可以只声明一次,自动作用于后面

// ---- myMath.h ----
extern "C" double calc( double );
// ---- myMath.C ----
// 在 Math.h 中的calc() 的声明
#include "myMath.h"
// 定义了 extern "C" calc() 函数
// calc() 可以从C 程序中被调用
double calc( double dparm ) { // ...
Loading Disqus comments...
Table of Contents