0

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?

6
  • storage is allocated but value not assigned. Commented Jul 15, 2014 at 6:48
  • Storage is allocated but value not assigned is declaration or definition? Commented Jul 15, 2014 at 6:51
  • In gcc, it prints garbage values. Does not throw compile time error Commented Jul 15, 2014 at 6:51
  • 1
    Have a look at difference-between-a-definition-and-a-declaration. Commented Jul 15, 2014 at 6:52
  • 3
    Line 2 is an assignment, not a definition. Line 1 is a definition. Commented Jul 15, 2014 at 6:53

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.