簡體   English   中英

從 c 中的另一個文件調用 function

[英]call a function from another file in c

我有兩個文件test-subseq.cmaxSeq.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM