What is the difference between a definition and a

2018-12-30 23:00发布

The meaning of both eludes me.

22条回答
无色无味的生活
2楼-- · 2018-12-30 23:22

According to the GNU C library manual (http://www.gnu.org/software/libc/manual/html_node/Header-Files.html)

In C, a declaration merely provides information that a function or variable exists and gives its type. For a function declaration, information about the types of its arguments might be provided as well. The purpose of declarations is to allow the compiler to correctly process references to the declared variables and functions. A definition, on the other hand, actually allocates storage for a variable or says what a function does.

查看更多
余欢
3楼-- · 2018-12-30 23:23

Declaration: "Somewhere, there exists a foo."

Definition: "...and here it is!"

查看更多
梦寄多情
4楼-- · 2018-12-30 23:23

Stages of an executable generation:

(1) pre-processor -> (2) translator/compiler -> (3) linker

In stage 2 (translator/compiler), declaration statements in our code tell to the compiler that these things we are going to use in future and you can find definition later, meaning is :

translator make sure that : what is what ? means declaration

and (3) stage (linker) needs definition to bind the things

Linker make sure that : where is what ? means definition

查看更多
泛滥B
5楼-- · 2018-12-30 23:27

Rule of thumb:

  • A declaration tells the compiler how to interpret the variable's data in memory. This is needed for every access.

  • A definition reserves the memory to make the variable existing. This has to happen exactly once before first access.

查看更多
登录 后发表回答