assert的使用方法以及extern的使用

来源:互联网 发布:尤克里里电脑调音软件 编辑:程序博客网 时间:2024/06/08 02:10

1. assert 宏的原型定义在<assert.h>,其作用是如果他的条件返回错误,则终止程序执行。

2. extern 的使用

首先,作为extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。例如,如果模块B欲引用该模块A中定义的全局变量和函数时只需包含模块A的头文件即可。这样,模块B中调用模块A中的函数时,在编译阶段,模块B虽然找不到该函数,但是并不会报错;它会在连接阶段中从模块A编译生成的目标代码中找到此函数
extern "C"是连接申明(linkage declaration),被extern "C"修饰的变量和函数是按照C语言方式编译和连接的,来看看C++中对类似C的函数是怎样编译的:
作为一种面向对象的语言,C++支持函数重载,而过程式语言C则不支持。函数被C++编译后在符号库中的名字与C语言的不同。举例:

function.h  #include<stdio.h>  extern int func(); 
function.c  #include "function.h"  int func()  {}
top.h#include <iostream>using namespace std;
main.cpp#include "top.h"extern "C"{#include "function.h"}int main(){func();}
在c++ 中嵌入 c代码的时候,头文件前面要加extern "C"不加,就会产生错误。
for (int i = 1; i <=9; i++)    {        for (int j = 1; j <=i; j++)        {            cout << i << "*" << j << "=" << i*j << " ";        }        cout << endl;    }
 for (int i = 1; i <9; i++)    {        for (int j = 1; j <i-1; j++)        {            cout << " * " ;        }        cout << endl;    }
for (int i = 8; i >0; i--)    {        for (int j = 1; j <i-1; j++)        {            cout << " * " ;        }        cout << endl;    }    









0 0