1. What is the output of following C program ?
int main()
{
func(5);
return 0;
}
func(int a)
{
int a;
a=10;
printf("%d",a);
}
=> This will end up in error.
int main()
{
func(5);
return 0;
}
func(int a)
{
int a;
a=10;
printf("%d",a);
}
=> This will end up in error.
error: 'a' redeclared as different kind of symbol
Reason: As int a is a local variable it will be stored in stack, when once
again a is re-declared it will thrown an error.
2.What is the output of following C program ?
int a=5;
int main()
{
func(5);
return 0;
}
func(int a)
{
a=10;
printf("%d",a);
}
Output => 10
Reason: In statement int a =5; a will be stored in data segment of memory,
where as int a in func(int a) will be stored in stack. So no redeclaration
scenario like earlier case.
3. In the below program 'a' is a global variable. So it can be accessed by
all functions.Is there any way to restrict the access of variable 'a' from
func3() ?
int a;
int main()
{
func1();
func2();
func3();
return 0;
}
func1()
{
}
func2()
{
}
func3()
{
}
Answer => Yes. Look into the previous example. By declaring 'a' inside func3()
we can restrict access to global variable 'a'
int a;
int main()
{
func1();func2();func3();
return 0;
}
func1()
{
}
func2()
{
}
func3()
{
int a;
}
4. What is the output of the following two programs?
int main()
{
char *p = NULL;
printf("%d %d",p,p++);
return 0;
}
Output => 0 1
Reason: NULL is always stored in the zeroth location. Increment pointer of char type
will shift to position 1 as char takes 1 byte.
int main()
{
int *p = NULL;
printf("%d %d",p,p++);
return 0;
}
Output => 0 4
Reason: NULL is always stored in the zeroth location. Increment pointer of int type
will shift to position 4 as int takes 4 byte.
5. What is the prototype of malloc and free ?
Answer =>
void *malloc(size_t size);
void free(void *ptr);
size_t is of unsigned type.
No comments:
Post a Comment