c语言程序有几个文件夹,如何将C程序拆分成多个文件?

通常,您应该在两个单独的.c文件(例如,A.c和B.c)中定义函数,并将它们的原型放在相应的头文件中(A.h,B.h,记住

include guards).

每当在.c文件中你需要使用另一个.c中定义的函数时,你将#include相应的头文件;那么你将能够正常使用这些功能.

必须将所有.c和.h文件添加到项目中;如果IDE询问您是否必须编译,则应仅标记.c进行编译.

快速举例:

Functions.h

#ifndef FUNCTIONS_H_INCLUDED

#define FUNCTIONS_H_INCLUDED

/* ^^ these are the include guards */

/* Prototypes for the functions */

/* Sums two ints */

int Sum(int a, int b);

#endif

Functions.c

/* In general it's good to include also the header of the current .c,

to avoid repeating the prototypes */

#include "Functions.h"

int Sum(int a, int b)

{

return a+b;

}

MAIN.C

#include "stdio.h"

/* To use the functions defined in Functions.c I need to #include Functions.h */

#include "Functions.h"

int main(void)

{

int a, b;

printf("Insert two numbers: ");

if(scanf("%d %d", &a, &b)!=2)

{

fputs("Invalid input", stderr);

return 1;

}

printf("%d + %d = %d", a, b, Sum(a, b));

return 0;

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值