關於c語言既#include

2009-03-16 11:29 pm
究竟最上面既#include有咩用?
eg.#include<stido.h>
#include<string.h>
有時試過唔洗打呢d都run到個program
其實真實既用途係咩?
更新1:

有人同我講過話寫program至少一定要打#include 呢句我試過唔洗打都run到啵 究竟入面有邊d function係需要用到先要打上去?

回答 (3)

2009-03-31 6:30 am
✔ 最佳答案
Can I answer this in English?

2009-03-30 22:30:26 補充:
究竟最上面既#include有咩用?

The lines with "#" in C is for the C preprocessor "cpp". Before the compiler will process the source file, the preprocessor will process the "macros" of the file. All macros are lines begin with "#" character.

Good example is to use the math functions. Without the #include <math.h> line, you may not be able to get a good a.out file and will not get a clean compile.

To see what the output after preprocessing looks like, you can use
> cpp somefile.c

You can think of using the include lines are just short hands not to type in all those function declarations. However, there are more to just that. #include lines can also be in the middle of a program and there are more than just include. It can be #define , #if ...

有時試過唔洗打呢d都run到個program
其實真實既用途係咩?
Sometimes without using any include lines, the compilation will work. It is because C will assume all functions return int and takes int by default. It is the same reason you can compile and specify no libraries (just cc somefile.c). That will work because by default C will link with libc in /usr/lib or /lib. This (just cc somefile.c) may not work if you use any functions that is not in the standard library. A good example is sin().

究竟入面有邊d function係需要用到先要打上去?
Not necessary. Many programmers (including myself) put in more than enough includes just in case. This will waste compile time but there are not harm done to the output. However, in a large or complex environment, you may have problem with this approach. It is a good habit of using the right amount of includes (or #define ...) in a source file.

Under DOS/Windows environment, things are a bit different (such as location of the files) but the idea is the same.
2009-03-31 12:45 am
yes
2009-03-17 5:19 am
#include <呢個位係library>

即係話如果你要用果個library裡面既function, 就要係上面declare..


如果你唔使用到既, 就唔使declare...
參考: 我=.=v


收錄日期: 2021-04-30 12:59:59
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090316000051KK00800

檢視 Wayback Machine 備份