Differences between declarations and definitions, significance of external declarations

Asked 2 years ago, Updated 2 years ago, 31 views

inta;←declaration

intb = 10; ← Declarations and Definitions

intc;
c=100;← What is this declaration and what is it?

Also, I don't understand the need for an external declaration...
Don't you just have to discuss it in advance and define specific variables?

c

2022-09-30 17:20

4 Answers

First of all, C and C++ languages are different languages and should be distinguished correctly.Also, the meaning of writing in and out of a function is different.

 inta;

Within a function, local variable declarations and definitions are valid only within the function (running the function).

If it is out of function, it is the declaration and definition of the global variable.static Valid only in the source file declared that you specify a storage class.extern You only declare the global variable and it is not defined.In this case, you can use this global variable, but if it is not defined elsewhere, it will result in a compilation error (to be exact, a link error) and you cannot generate an executable file.Conversely, repeated global variable definitions and declarations across multiple locations without extern also result in a link error.
Only one declaration and definition, such as inta;, must be made for the global variable to be compiled correctly, and only the declaration, such as externinta;, must be made for the rest.

intb=10;

Declarations and definitions, plus initialization.It can be written either inside or outside the function.

intc;
c = 100;

The first line is declaration and definition.The second line is substituted and cannot be written outside of the function.


2022-09-30 17:20

Also, I don't understand the need for an external declaration...

About
What is the declaration of the external variable xx?

There is no substance in this translation unit (source), but
First of all, please compile on the assumption that there is.
Since the entity of variable xx is in another source,
"If any obj finds you at the link, use it."
That's right.

"Regarding the need, ""How to use variables that are substantial outside of your translation unit"" as described above."
That's right.


2022-09-30 17:20

The correct values are as follows:
Description for global variables.

inta;
Declaration and definition.
An int-shaped container named a is made.
The value of a varies by environment.

intb=10;
Declaration and definition.
An int-shaped container named b is made.
The value of b is 10.

c=100;
It's not a declaration or definition, it's a substitution.

external intd;
Declaration.
The container named d is not created, so you must intd; in order to read and write d.In other words, use external to access d defined in another file.


2022-09-30 17:20

intc;
c=100;← What is this declaration and what is it?

The declared variable (c here) is subsidized.


2022-09-30 17:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.