天天看點

#include<file.h>與#include"file.h"的差別

對于這種問題先來看一看外國人怎麼想的,因為本來這種語言就是他們創造的,http://www.geekinterview.com/question_details/3379

what is the difference between #include <file> and #include “file”?

when writing your c program, you can include files in two ways. the first way is to surround the file you want to include with the angled brackets < and >. this method of inclusion tells the preprocessor to look for the file in the predefined default location.

this predefined default location is often an include environment variable that denotes the path to your include files. for instance, given the include variable

include=c:compilerinclude;s:sourceheaders;

using the #include <file> version of file inclusion, the compiler first checks the c:compilerinclude

directory for the specified file. if the file is not found there, the compiler then checks the

s:sourceheaders directory. if the file is still not found, the preprocessor checks the current directory.

the second way to include files is to surround the file you want to include with double quotation marks. this

method of inclusion tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations you have set up. using the #include “file” version of file inclusion and applying

it to the preceding example, the preprocessor first checks the current directory for the specified file. if the

file is not found in the current directory, the c:compilerinclude directory is searched. if the file

is still not found, the preprocessor checks the s:sourceheaders directory.

the #include <file> method of file inclusion is often used to include standard headers such as stdio.h or

stdlib.h. this is because these headers are rarely (if ever) modified, and they should always be read from your

compiler’s standard include file directory. 

the #include “file” method of file inclusion is often used to include nonstandard header files that you have created for use in your program. this is because these headers are often modified in the current directory, and you will want the preprocessor to use

your newly modified version of the header rather than the older, unmodified version.

總的來說:

當用#include“file.h”時,先搜尋目前工作目錄,如果沒有,再去搜尋庫,庫沒有再搜尋資源庫;

當用#include<file.h>時,編譯器先從标準庫路徑開始搜尋,如果沒再搜尋資源庫目錄,最好搜尋目前工作目錄。

繼續閱讀