Reputation: 852
Suppose I have the following program:
int main(void)
{
int i; //Line 1
i=5; //Line 2
int *j; //line 3
j=&i; //line 4
}
If I try to print i after line 1 in Visual Studio, it gives me a compile time error saying unitialized variable i used. Does this mean that no storage was allocated to i and line 1 is only a declaration? I understand line 2 is a definition.
Also, what about line 3 and line 4? Are they declarations or definitions?
Upvotes: 0
Views: 76
Reputation: 122493
Line 1 and Line 3 are definitions, It's also legal to say they are declarations because all definitions are declarations.
The error is because using uninitialized variables is undefined behavior, not because their storage are not allocated.
Line 2 and Line 4 are assignment statements. You seem to be confused with initalization and assignment.
int n = 42; //definition with initalization
int m; //definition, but uninitiazlied
n = 10; //assignment
m = 10; //assignment
Upvotes: 5