In the book C++ Primer, it states: A variable definition consists of a type specifier, followed by a list of one or more variable names separated by commas, and ends with a semicolon. Each name in the list has the type defined by the type specifier. A definition may (optionally) provide an initial value for one or more of the name it defines.
Now my confusion is that, I've always thought that for built-in types like int, double, float, etc. If I don't explicitly initialize the variable, then that's simply a declaration. For example:
int a; //i thought that this would just be a declaration
int a = 45; //while built-in types with explicit initialization would be a
//definition
Especially, since default initialization states that: The value of an uninitialized variable of a built-in type is undefined. (when inside a function)
Yet the book says:
int a; //is a definition (both a declaration and definition)
int b; //is a definition. yet the variable b is undefined.
So now i'm kind of confused as to what constitutes a declaration and what constitutes a definition? if having and not having an explicit initialization is not the determining factor.
I know the difference between a declaration and definition when it comes to functions/class. That to use a function/class in more than one file requires declarations that are separate from the function's definition. Where we define the function in only one file (header file). And that other files that use that function/class must declare (not define) when using that class/function.
In addition, I know that:
A declaration specifies the type and name of a variable and makes a name known to the program.
While, a definition creates the associated entity and allocates storage for that particular variable.
But still, i'm a bit puzzled about when something is a definition vs a declaration when it comes built-in types. Basically all built in types with or without an initializer is both a declaration and a definition? Example:
int x;
double y;
char z;
float g;