recategorized by
270 views
0 votes
0 votes
Explain the behaviour of following code:

int main()

{

int *j=0;

{

int i=10;

j=&i;

}

printf("%d",*j);

}

 

a. output is 10. j pointed to address of i, so it was not freed.

b. output may be 10 or garbage in given execution

c. output is 10. i becomes invisible outside of its block scope, but lives as long as function scope

d. output is 0.
recategorized by

1 Answer

2 votes
2 votes
When a variable goes out of scope, it's memory location is no longer valid. Accessing it causes undefined behaviour in C.

Related questions

0 votes
0 votes
1 answer
2
Harikesh Kumar asked Dec 4, 2017
358 views
n=3a[++n]=n++ Output : a[4]=4. How?Please explain it
0 votes
0 votes
3 answers
3
1 votes
1 votes
2 answers
4
go_editor asked Jul 10, 2016
2,369 views
Which API is used to draw a circle?Circle ()Ellipse ()Round Rect ()Pie ()