Just started with K & R and on the 2nd chapter, there is the line:
Declarations list the variables to be
used and state what type they have and
perhaps what their initial values are.
So:
int x = 42 is a definition.
and int x is a declaration but also a definition since every definition is a declaration.
But when we assign an intial value like K & R say, doesn’t that make the declaration a definition?
You confuse two things:
* object as in: variable, function etc., not an OOP object.
A definition is hence very often also a declaration, since you cannot define what is in an object when you do not state what the type of the object is. Easiest to remember is just: “Every definition is a declaration, but not every declaration is a definition”
For variables
There is only 1 way to declare without defining the variable:
This tells the compiler that there is a variable called variable_name with type typeX, but not where to get it.
Every other way to declare a variable is also a definition, since it tells the compiler to reserve space for it and perhaps give it an initial value.
The difference is much clearer in structs and functions:
For Structs
A declaration:
This declares some_struct to the compiler with a and b as struct variables both with type int.
Only when you define them space is reserved and you can use them:
For functions:
The difference is much more clear
declaration:
A definition could be like the one above (in the struct part)