小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C語言中,函數(shù)不申明也能使用,但會出現(xiàn)warning: implicit declarat...

 敗敗0619 2012-09-14

這段時間,在看中心后臺服務(wù)軟件源碼時發(fā)現(xiàn),有很多自定義函數(shù)未經(jīng)申明卻能在主程序中被調(diào)用,主程序中沒有包括上述函數(shù)的頭文件,我在各個目錄中也找不到上述函數(shù)的頭文件。這就奇怪了,連使用標(biāo)準(zhǔn)庫函數(shù)printf()都要包括標(biāo)準(zhǔn)輸入輸出頭文件<stdio.h>,何況是自定義函數(shù)?這個問題困擾了我很久。前天問中創(chuàng)公司奚鐘華,他說:確實沒有頭文件,它也試過,在原來的目錄中編寫了一些新的函數(shù),照葫蘆畫瓢地在Makefile中添加了相應(yīng)項,生成了庫文件,在主程序中就可調(diào)用了,其中的機(jī)理它也說不清。

今天通過實驗,基本明白了個中原因。

一、在VC6中,文件test1.cpp如下:

 

#include "stdafx.h"           // stdafx.h: #include <stdio.h>

 

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

int main(int argc, char* argv[])

{

       int a = 300, b = 100;

       printf("Max(a, b): %d/n", Max(a, b));

       return 0;

}

 

程序正常運行,因為Max()在被調(diào)用前已經(jīng)定義。

 

Max()移到main()之后:

#include "stdafx.h"           // stdafx.h: #include <stdio.h>

 

int main(int argc, char* argv[])

{

       int a = 300, b = 100;

       printf("Max(a, b): %d/n", Max(a, b));

       return 0;

}

 

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

編譯時出錯:

error C2065: 'Max' : undeclared identifier

error C2373: 'Max' : redefinition; different type modifiers

 

main()之前添加Max()的聲明:

#include "stdafx.h"           // stdafx.h: #include <stdio.h>

 

int Max(int x, int y);

 

int main(int argc, char* argv[])

{

       int a = 300, b = 100;

       printf("Max(a, b): %d/n", Max(a, b));

       return 0;

}

 

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

程序正常運行,因為Max()在被調(diào)用前已經(jīng)定義。

 

二、在Linux中,文件test2.c如下:

#include <stdio.h>

 

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

int main(int argc, char* argv[])

{

       int a = 300, b = 100;

       printf("Max(a, b): %d/n", Max(a, b));

       return 0;

}

 

gcc編譯:gcc -Wall -o test2 test2.c,通過。程序正常運行,因為Max()在被調(diào)用前已經(jīng)定義。

 

Max()移到main()之后:

#include <stdio.h>

 

int main(int argc, char* argv[])

{

       int a = 300, b = 100;

       printf("Max(a, b): %d/n", Max(a, b));

       return 0;

}

 

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

gcc編譯:gcc -Wall -o test2 test2.c,出現(xiàn)警告:

warning: implicit declaration of function `Max'

仍然編譯通過,程序也能正常運行,因為在C語言中,當(dāng)函數(shù)在調(diào)用函數(shù)之前沒有聲明或定義,默認(rèn)作為隱式聲明處理,只要在調(diào)用函數(shù)之后定義,或在別的模塊中定義并編譯成庫文件,該庫文件在調(diào)用函數(shù)所屬模塊編譯時載入,程序即可正常運行。

 

g++編譯:g++ -Wall -o test2 test2.c,出現(xiàn)錯誤和警告:

error: `Max' was not declared in this scope

warning: unused variable 'Max'

沒有生成可執(zhí)行程序test2。因為g++使用C++的規(guī)則:函數(shù)在被調(diào)用前必須聲明或定義。

 

三、在Linux中,采用實際工程的方式(分成若干模塊)進(jìn)一步實驗,驗證了C語言中函數(shù)在被調(diào)用前不申明也能使用。

1、在/u01/work/tools目錄中,編寫4個文件:

Max.c

int Max(int x, int y)

{

       return ((x > y ) ? x : y);

}

 

Min.c

int Min(int x, int y)

{

       return ((x > y ) ? y : x);

}

 

Add.c

int Add(int x, int y)

{

       return (x + y);

}

 

Makefile

#User defined library, by He Yaozhong

CC=gcc

HOME=/u01/work

OS=$(HOME)/tools

 

INCLUDE=$(HOME)/headfile -I$(HOME)/tools

 

CSTARGETS=Max.o Min.o Add.o

 

LIBS=$(HOME)/lib/tools.a

 

all: $(LIBS) clean

 

 

Max.o : Max.c

       $(CC) -I$(INCLUDE) -c $(CCFLAGS) Max.c

 

Min.o : Min.c

       $(CC) -I$(INCLUDE) -c $(CCFLAGS) Min.c

 

Add.o : Add.c

       $(CC) -I$(INCLUDE) -c $(CCFLAGS) Add.c

 

$(LIBS) : $(CSTARGETS)

       cd $(OS); /

       ar ruv $(LIBS) $(CSTARGETS:$HOME/lib/=)

 

clean:

       rm -f *.o

 

/u01/work/tools目錄中,使用make工具:

[root@node01 tools]# make

gcc -I/u01/work/headfile -I/u01/work/tools -Wall -c  Max.c

gcc -I/u01/work/headfile -I/u01/work/tools -Wall -c  Min.c

gcc -I/u01/work/headfile -I/u01/work/tools -Wall -c  Add.c

cd /u01/work/tools; /

ar ruv /u01/work/lib/tools.a  Max.o Min.o Add.o

r - Max.o

r - Min.o

r - Add.o

rm -f *.o

生成了/u01/work/lib/tools.a庫文件。

 

2、在/u01/work/test2目錄中,編寫2個文件:

test2.c

#include <stdio.h>

 

int main(int argc, char* argv[])

{

       int a, b;

 

       printf("Please input 2 integer (a, b): /n");

       scanf("%d", &a);

       scanf("%d", &b);

      

       printf("Max(a, b): %d/n", Max(a, b));

       printf("Min(a, b): %d/n", Min(a, b));

       printf("Add(a, b): %d/n", Add(a, b));

      

       return 0;

}

 

Makefile

CC=gcc

HOME=/u01/work

 

INCLUDE=-I$(HOME)/headfile -I.

 

CFLAGS= -L$(HOME)/lib

 

LIBS=$(HOME)/lib/tools.a

 

all : $(OBJS) test2 clean

 

test2 : test2.c

       $(CC) $(INCLUDE) -Wall -o test2 test2.c $(CFLAGS) $(LIBS)

 

clean :

       rm -f *.o

 

/u01/work/test2目錄中,使用make工具:

[root@node01 func_pointer]# make

gcc  -I/u01/work/headfile -I.  -Wall -o test2 test2.c -L/u01/work/lib /u01/work/lib/tools.a

test2.c: In function `main':

test2.c:11: warning: implicit declaration of function `Max'

test2.c:12: warning: implicit declaration of function `Min'

test2.c:13: warning: implicit declaration of function `Add'

rm -f *.o

生成了/u01/work/test2/test2可執(zhí)行文件。運行程序:

[root@node01 func_pointer]# ./test2

Please input 2 integer (a, b):

200

300

Max(a, b): 300

Min(a, b): 200

Add(a, b): 500

 

結(jié)果完全正確。

 

四、小結(jié)

CC++是強類型語言,變量類型均應(yīng)在代碼執(zhí)行前確定。

在函數(shù)聲明方面CC++則不同,C++語言中,在被調(diào)用之前未聲明或定義是不允許的,而C語言是允許的。初看起來C語言這一特性是靈活、省事,但缺點是:

1、程序可讀性差。

2、易出錯。函數(shù)先聲明再調(diào)用,是一種糾錯機(jī)制,如果不聲明,則沒有用到這種糾錯機(jī)制,雖然編譯、連接通過,程序也能運行,但很可能結(jié)果不正確。

 

一個好的程序員,應(yīng)該養(yǎng)成嚴(yán)謹(jǐn)、規(guī)范的編程習(xí)慣,編譯程序時應(yīng)打開顯示所有警告選項“-Wall”,并且對編譯器提出的所有警告都要給予處理,僅以編譯、連接通過為目標(biāo),這可能會有潛在的危害。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多