Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between declaration and definition in programming?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what's the difference between declaration and definition in programming". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "what is the difference between declaration and definition in programming".

General explanation: a declaration tells the compiler that there is such an identifier. The definition is to apply for a piece of memory for the program.

For example:

Int x; / / this is a definition extern int x; / / this is the declaration int x = 10; / / this is also a definition

The use of extern int x = 10 in the program is also defined.

Declared operations are typically used in header files so that the same variable can be referenced in multiple source files. This also shows why definitions cannot be used. Because after the header file is included in the source file, it actually becomes part of the source file.

So, if you define a variable in the header file, the definition of the variable will appear in multiple source files. The so-called repetitive definition.

The above said that variables cannot be defined in the header file. But there are three exceptions. Header files can define classes, const objects whose values are known at compile time, and inline functions.

These entities are defined in the header file (because they are not just declared) because the compiler needs their definitions to generate code.

For example, in order to generate code that defines and uses objects of a class, the compiler needs to know the data members that make up the class, as well as the operations that can be performed on those objects. The definition of the class provides this information, so you need to define the class in the header file

Because the const variable defaults to the local variable of the file in which it is defined. So the definition of this variable is legal even if it appears in multiple source files.

1. When the const variable is initialized with a constant expression, its initialization process can be implemented in the header file.

2. If the const variable is not initialized with a constant expression, it cannot be initialized in the header file, but rather defined and initialized in the source file. Then add the extern declaration to the header file so that it can be shared by multiple files.

The above is all the content of the article "what's the difference between declaration and definition in programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report