Lifetime and visibility of a variable

main(){
int *j;
{
int i=10;
j=&i;
}
printf(“%d”,*j);
}
Answer:
10
The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i.

Leave a Reply

Your email address will not be published. Required fields are marked *