0

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;  
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
FormosaTBM
  • 41
  • 1
  • 2
    As a side note: definition is also a declaration. – Ron Jul 24 '17 at 09:28
  • 1
    It says it in your first paragraph quote, "A definition may (optionally) provide an initial value for one or more of the name it defines." . So why would you continue to think that it is not a definition if there is no initializer? – M.M Jul 24 '17 at 09:37
  • the marked duplicate is a C question but this is a C++ question - suggest finding better links to add – M.M Jul 24 '17 at 09:40

2 Answers2

1

int a; is both a declaration (the variable can be used) and a definition (it has its own memory) but if it is an automatic variable, it is not initialized.

extern int a; is a mere declaration and is not a definition.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
-2

From here:

Most of the time, when you declare a variable, you are also providing the definition. What does it mean to define a variable, exactly? It means you are telling the compiler where to create the storage for that variable. For example, if you write:

int x;
int main()
{
    x = 3;
}

The line int x; both declares and defines the variable; it effectively says, "create a variable named x, of type int. Also, the storage for the variable is that it is a global variable defined in the object file associated with this source file

this is a part from site more relevant to your question please refer to the link for further your misconception will be hopefully cleared http://www.cprogramming.com/declare_vs_define.html

Ashish Garg
  • 68
  • 1
  • 13