課程
/后端開發(fā)
/C
/C語言入門
為什么這里printLine函數前邊不能省略extern
2016-06-26
源自:C語言入門 5-13
正在回答
因為文件hello.c的printLine()函數和test.c中的say()函數是被相互引用的,因此這兩個函數都應為外部函數,在兩個文件中都必須聲明引用。所以小編的答案是錯誤的!具體代碼如下:
補充:extern的主要作用不在于定義外部變量或函數,而在于引用定義,如果在其他編譯器中去掉#include”test.c",不然會出現重定義(例如visual studio)。
hello.c文件中:
#include <stdio.h>#include "test.c"?? //引用test.c文件extern void say();void printLine()???? //這里定義的方法對嗎?{?? printf("**************\n");???}int main(){??? say();??? return 0;}
test.c文件中:
#include <stdio.h>extern void printLine();void say(){??? printLine();??? printf("I love imooc\n");??? printf("good good study!\n");??? printf("day day up!\n");??? printLine();}
fenx_cc 提問者
舉報
C語言入門視頻教程,帶你進入編程世界的必修課-C語言
2 回答不是說外部函數extern可以省略么?
1 回答C語言規(guī)定,在沒有指定函數的作用范圍時,系統(tǒng)會默認認為是外部函數,因此當需要定義外部函數時extern也可以省略。
1 回答可以省略extern嗎
1 回答為什么要在printline定義外部函數呢?應該在say前面定義外部函數吧?
1 回答把倆函數都定義成外部函數
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-06-26
因為文件hello.c的printLine()函數和test.c中的say()函數是被相互引用的,因此這兩個函數都應為外部函數,在兩個文件中都必須聲明引用。所以小編的答案是錯誤的!具體代碼如下:
補充:extern的主要作用不在于定義外部變量或函數,而在于引用定義,如果在其他編譯器中去掉#include”test.c",不然會出現重定義(例如visual studio)。
hello.c文件中:
#include <stdio.h>
#include "test.c"?? //引用test.c文件
extern void say();
void printLine()???? //這里定義的方法對嗎?
{
?? printf("**************\n");???
}
int main()
{
??? say();
??? return 0;
}
test.c文件中:
#include <stdio.h>
extern void printLine();
void say(){
??? printLine();
??? printf("I love imooc\n");
??? printf("good good study!\n");
??? printf("day day up!\n");
??? printLine();
}