S
somenath
Hi All ,
I have one question regarding scope and lifetime of variable.
#include <stdio.h>
int main(int argc, char *argv[])
{
int *intp = NULL;
char *sptr = NULL;
{
int i =5;
char *s = "hello";
intp = &i;
sptr = s;
}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);
return 0;
}
Out put of the program
value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?
Regards,
Somenath
I have one question regarding scope and lifetime of variable.
#include <stdio.h>
int main(int argc, char *argv[])
{
int *intp = NULL;
char *sptr = NULL;
{
int i =5;
char *s = "hello";
intp = &i;
sptr = s;
}
printf("value of i = %d\n",*intp);
printf("value of sptr = %s \n",sptr);
return 0;
}
Out put of the program
value of i = 5
value of sptr = hello
The variable i, and s has block scope .But as s points to string
literal life time of the s is through out the program .
My question is as i has only block scope so once it goes out of scope
address of i becoming meaning less. So is it guaranteed that first
printf statement will always print 5 ?
Regards,
Somenath