[英]call a function from another file in c
我有兩個文件test-subseq.c
和maxSeq.c
。 在test-subseq.c
中, test_maxSeq
調用maxSeq
,所以我需要在test-maxSeq.c
中添加maxSeq
的原型,以便編譯器知道 ZC1C4252674C178A。 在maxSeq.c
中, maxSeq
調用max
。 我的問題是,在test-maxSeq.c
中,我是否還需要為max
添加原型?
// maxSeq.c
size_t max(size_t a, size_t b) {
// returns the larger number between a and b
}
size_t maxSeq(int * array, size_t n) {
/*
* returns the length of the maximum increasing contiguous subsequence
* in the array and call max
*/
}
// test-subseq.c
size_t maxSeq(int * array, size_t n);
void test_maxSeq(int * array, size_t n, size_t expt) {
size_t ans = maxSeq(array, n);
assert(ans == expt)
}
int main(void) {
int array0 = {1, 2, 1, 3, 5, 7};
test_maxSeq(array0, 6, 4);
return 0;
}
不,test-subseq.c 只需要聲明它將專門調用的函數。
但是,正如評論者所說,最好將您的聲明放在 header 文件中,並將其#include
放在 test-subseq.c 中。
將 maxSeq 放入 header 文件中,然后將 header 文件包含在 test-subseq.c 中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.