extern
-
在声明变量的时候不能加初始值
extern int a =10;//错误
在访问other.cpp的全局函数时,extern可以不写
在访问other.cpp的全局变量时,必须加上extern
多文件项目的生成
- 编译:将多个cpp文件转化为中间文件*.obj
- 链接:将多个obj文件转化为*.exe可执行程序
头文件#include指令
- 将类的定义与函数名称写在*.h里面,将函数具体实现写在other.cpp里面,主函数为main.cpp
宏定义#define
所有#开头的指令都是预处理指令
-
在预处理时,采用的是原文替换,#include就是如此
#define n 1+2 int main() { int a = 4 * n; }
此时n不是3,而是原文替换为1+2,即a = 4 * 1 + 2,a的值为6
-
条件编译指令
#if #endif #ifdef #endif
-
头文件的重复包含问题
#include "Object.h" #include "Object.h"//重复包含相同的头文件
#ifndef _OBJECT_H #define _OBJECT_H ... #endif
main函数的参数和返回值
-
main函数的标准形式
int main() int main(int argc,char* argv[])
argc表示命令行参数的个数,argv表示命令行参数的值
在命令行窗口运行这个程序
#include <stdio.h> int main(int argc, char** argv) { for(int i=0;i<argc;i++) { printf("%s\n",argv[i]); } }
命令行切换到* .exe所在目录,输入.exe 123 abc 456 def
会依次输出*.exe 123 abc 456 def
这说明argc值是5,argv[]存储的字符串就是以上字符
static修饰变量
- 在other.cpp中用static修饰一个变量,该变量称为静态变量,仅能在other.cpp内可见
- 此时在main函数中不能用extern来访问它,static修饰函数时同理