What is the clear difference between declaration and definition?

Asked 2 years ago, Updated 2 years ago, 156 views

What is the clear difference between declaration and definition?
For example, can I consider the following examples as both definitions?

#define a100
#define ba

c++ c preprocessor

2022-09-30 19:37

4 Answers

Declaration Definition is "declaration" or "definition" in English.
I have a question that exactly applies to my stackoverflow.

Simply put, the syntax element required by the compiler to resolve the reference is a declaration, and the syntax element required by the linker to link the reference is a definition.For more information, please look at our questions and answers.The code that the questioner illustrated in the example is "preprocessor directive" as the other answers say, and is different from the definition or declaration.

A clear difference between definitions and declarations is that the same identifier "declaration" can exist separately for each compilation unit, so it can appear multiple times in a program, but there is a rule called One Definition Rule in the definition.


2022-09-30 19:37

Declarations and definitions are C++ and C language concepts, but the #define statement is a syntax for the preprocessor, not strictly C++ or C language.
Naturally, the examples given are neither definitions nor declarations, as they are macro-expanded with little or no linguistic significance.


2022-09-30 19:37

The Declaration tells the compiler to use this name.Definition tells you the name and its contents together.Therefore, "Definition" is "declaration", but "declaration" does not necessarily mean "definition".

For the sake of explanation below, we will write "just a declaration" for a declaration that is not a definition.

Declarations have no information about what's inside, so if you try to do something that you can't do without knowing what's inside, you get a compilation error or a link error.

structure my_structure;// Just a declaration

my_struct*p;//OK; pointer can be held without knowing what's inside
my_structures; // compilation error; I don't know the memory size I need if I don't know what's inside.
p->a;// compilation error;not sure if a is really a member

For Functions

bool f1(inta);// Just a declaration

void f2(intb)
{
    if(f1(b)){//OK;can be called without knowing what's inside at compile time
        // processing of something
    }
}

However, if f1() is not defined anywhere else, a link error occurs.

Just a few examples of declarations and definitions.

 inta;// definition
external intb;// Just a declaration
external intc=3;// definition; external but initialized

structs1;// Just a declaration
structs2 {
    inta;
    intb;
};                  // Definition

typeef int my_int;// Just a declaration

intf(inta);// Just a declaration
intf(inta)
{
    return a*a;
}                   Redeclaring and Defining //f

By the way, #define is neither a declaration nor a definition, as other respondents have said.The compiler itself is what the preprocessor handles before compiling the source.


2022-09-30 19:37

Everyone is so strict... well, Oira can't talk about people either.

ISO/IEC 14882:1998
16.3 contains
Identifier Defined as Macro
16.3.5 contains
Macro Definition Valid Range
16.8 contains the
Predefined Macro Name
#define can be called "macro definition" because of the wording.

There is no macro declaration, but it is also 16.3-5
"The provisional argument identifier must be uniquely declared (snip)"
Therefore, I think the phrase "declaration of provisional arguments within macro definition" is ant.

3.1 Declarations and definitions
The technical terms "declaration" and "definition" are as you describe them.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.