一、编译多源代码文件的程序
使用多个函数的最简单方法就是将它们放在同一文件中,然后像编译单个函数的文件一样对该文件进行编译。
1.UNIX
首先假定UNIX系统下安装了标准的编译器cc。文件file1.c和文件file2.c中包含有c函数,命令如下:
cc file1.c file2.c
另外还将生产两个目标文件file1.o和file2.o。如果随后只更改了文件file1.c而file2.c没有改变,可以使用命令编译第一个文件并将其链接到第二个文件的目标代码:
cc file1.c file2.o
2.LIUNX
同UNIX一样,如果装有编译器gcc,则命令为
gcc file1.c file2.c
3.DOS命令行编译器
大多数DOS命令行编译器和UNIX相同,不同在于DOS生产的目标文件扩展名为.obj而不是.o。而且有些编译器并不生产目标代码文件,而是生产汇编语言或者其他特殊的代码文件。
4.windows和苹果的IDE编译器
windows和macintosh系统下的编译器是面向工程的。工程描述了一个特定的程序所使用的资源。这些资源中包括源代码文件。编译器编译文件是对工程的编译。
5.使用头文件
二、头文件
1.头文件的定义
以.h为后缀的文件
一般而言,每个C++/C程序通常由头文件和定义文件组成。头文件作为一种包含功能函数、数据接口声明的载体文件,主要用于保存程序的声明,而定义文件用于保存程序的实现。
2.头文件的作用
头文件的主要作用在于多个代码文件全局变量(函数)的重用、防止定义的冲突,对各个被调用函数给出一个描述,其本身不需要包含程序的逻辑实现代码,它只起描述性作用,用户程序只需要按照头文件中的接口声明来调用相关函数或变量,链接器会从库中寻找相应的实际定义代码。
3.头文件的使用:
我们可以自己定义头文件,用来声明函数原型,声明预处理常量,这样我们就可以在任何的文件使用头文件从而达到使用预处理常量和函数原型的作用,避免重复使用它们而引发的错误,这样也有利于修改。总之,把函数原型和常量定义放在一个头文件中是一个很好的编程习惯。
(1)头文件的调用

include 指令会指示 C 预处理器浏览指定的文件作为输入。预处理器的输出包含了已经生成的输出,被引用文件生成的输出以及 #include 指令之后的文本输出。

include

include "my.h"

(2)头文件引用的格式如下:
格式1: #include <xxx.h>
格式2: #include "xxx.h"
第一种格式,头文件存放在一对尖括号< >内,表示引用系统提供的头文件,例如,stdio.h、stdlib.h头文件,是系统提供的头文件,所以,引用时,使用尖括号包含。
第二种格式,头文件存放在一对双引号 “” 内,表示 引用用户自定义的头文件。例如我们自己定义了一个test.h头文件,那么,就使用双引号包含
这两种格式的区别,就是编译器在编译代码的时候,快速查找到需要引用的头文件。例如,遇到第一种格式引用的头文件,就去系统的 /usr/include/ 等系统库目录下查找头文件。遇到第二种格式引用的头文件,就在源码当前目录,或者GCC编译器使用 -I 选项包含的目录下查找头文件。
4.程序示例
假定需要管理4个连锁的旅馆。每一个旅馆都有不同的收费标准,但是对于一个特定的旅馆,其中的所有房间都符合同一种收费标准。对于预定住宿时间超过一天的人来说,第二天的收费是第一天的95%,而第三天的收费是第二天的95%,等等。我们需要这样一个程序,即对于指定的旅馆和总的住宿天数可以计算出收费总额。同时程序需要第一个程序

include include "hotel.h"
int main(void)

{

int nights;

double hotel_rate;

int code;

while ((code = menu()) != QUIT)

{

switch(code)

{

case 1 : hotel_rate = HOTEL1;

break;

case 2 : hotel_rate = HOTEL2;

break;

case 3 : hotel_rate = HOTEL3;

break;

case 4 : hotel_rate = HOTEL4;

break;

default: hotel_rate = 0.0;

printf("Oops!\n");

break;

}

nights = getnights();

showprice(hotel_rate, nights);

}

printf("Thank you and goodbye.");return 0;

}


第二个程序

int getnights(void)

{

int nights;

printf("How many nights are needed? ");

while (scanf("%d", &nights) != 1)

{

scanf("%*s");

printf("Please enter an integer, such as 2.\n");

}

return nights;

}

void showprice(double rate, int nights)

{

int n;

double total = 0.0;

double factor = 1.0;

for (n = 1; n <= nights; n++, factor *= DISCOUNT)

total += rate * factor;

printf("The total cost will be $%0.2f.\n", total);

}


第三个程序

define QUIT       5
define HOTEL1    80.00
define HOTEL2   125.00
define HOTEL3   155.00
define HOTEL4   200.00
define DISCOUNT   0.95
define STARS "**********"
// 显示选择列表

int menu(void);

// 返回预订天数

int getnights(void);

// 根据费率、入住天数计算费用

// 并显示结果

void showprice(double rate, int nights);


下面是这个多文件程序的运行示例:


Enter the number of the desired hotel:
1. Fairfield Arms           2) Hotel
2. Chertworthy Plaza        4) The

5)quit

3

How many nights do you needed? 1

The total cost will be $255.00.

Enter the number of the desired hotel:
1. Fairfield Arms           2) Hotel
2. Chertworthy Plaza        4) The

5)quit

4

How many nights do you needed?

3

The total cost will be $1012.64.

Enter the number of the desired hotel:
1. Fairfield Arms           2) Hotel
2. Chertworthy Plaza        4) The
3. quit

5

Thank you and goodbye.

  1. 二、查找地址:&运算符
    指针(pointer),也就是用来存储地址的变量。一元运算符&可以取得变量的存储地址。假设pooh是一个变量的名字,那么&pooh就是该变量的地址。一个变量的地址可以被看做是改变量在内存中的位置。假定使用了以下语句:
    pooh=24;
    并且假定pooh的存储位置是0B76(PC的地址一般以3位十六进制数的形式表示)。那么语句:
    printf("%d%p\n",pooh,&pooh);
    将输出如下数值(%p是输出地址的说明符):
    240876
    /* loccheck.c -- checks to see where variables are stored /

include

void mikado(int);                      / declare function  /

int main(void)

{

int pooh = 2, bah = 5;             / local to main()   /

printf("In main(), pooh = %d and &pooh = %p\n",pooh, &pooh);

printf("In main(), bah = %d and &bah = %p\n",bah, &bah);

mikado(pooh);

return 0;

}

void mikado(int bah)                   / define function   /

{

int pooh = 10;                     /* local to mikado() */

printf("In mikado(), pooh = %d and &pooh = %p\n", pooh, &pooh);

printf("In mikado(), bah = %d and &bah = %p\n", bah, &bah);

}


其输出结果如下:
In mian(),pooh=2 and %pooh=0x0012ff48
In mian(),bah=5 and %bah=0x0012ff44
In mian(),pooh=10 and %pooh=0x0012ff34
In mian(),bah=2 and% bah=0x0012ff40
三、更改主调函数中的变量
x=y;
y=x;
可这样完全不起作用,因为执行到第二行的时候,x的原始值会被覆盖。因此我们需要多写一行代码。
temp=x;
x=y;
y=temp;
这三行代码便可以实现值交换的功能,下面编写一个驱动程序来测试一下

include

void interchange(int u,int v);

int main(void)

{

int x=5,y=10;

printf("原始数据x=%d and y=%d.\n",x,y);

interchange(x,y);

printf("现在x=%d and y=%d.\n",x,y);

return 0;

}

void interchange(int u,int v){

int temp;

temp=u;

u=v;

v=temp;

}


输出结果如下:
原始数据x = 5 and y = 10
现在x = 5 and y = 10
可以看到数据并未发生变化,这是为什么呢?
因为 interchange() 使用的变量并不是 main() 中的变量.因此交换u和v的值对x和y的值没有任何影响!
现在可以改变interchange()输出打印语句来得到我们想要的结果。

include void interchange(int u,int v);

int main(void)

{

int x=5,y=10;

printf("原始数据x=%d and y=%d.\n",x,y);

interchange(x,y);

printf("现在x=%d and y=%d.\n",x,y);return 0;

}

void interchange(int u,int v){

int temp;

printf("现在u=%d and v=%d\n",u,v);

temp=u;

u=v;

v=temp;

printf("现在u=%d and v=%d\n",u,v);

}


原始数据x = 5 and y = 10
原始数据u= 5 and v= 10
现在u = 10 and v = 5
现在x = 5 and y = 10