C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
Memory related question of c programming
(1) What will output:
void main()
{
int a=5,b=6,c=7;
printf(“%d,%d,%d”);
}
Ans:
Output: 7 6 5
Explanation:
Default sotrage class int a=5 is auto.Since it automatic variable it will create in the stack area.
It will store in the stack as
Stack always follows LIFO datastructure.
In the printf statement name of variable is not written explicitly.So default output will content of
Stack which will be in the LIFO order i.e 7 6 5.
(memory map)
(2)
what will be output:
void main()
{
int a=5 ,b,c=7;
printf(“%d %d %d”);
}
Ans:
Output: 7 5 garbage value
Explanation:
Automatic variable a and c has initialized while b has not initilize.Initialize variable are more nearer than non initialize variable .They will be stored in the stack.So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is persent in the stack.
(memory map)
(3)What will output :
Void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Ans:
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer. So it’s output will depends upon which memory model has selected. Default memory model is small.
More detail click here(pointer c)
(4)
What will be output ?
void main()
{
int huge *a=(int huge *)0x59990005;
int huge *b=(int huge *)0x59980015;
if(a==b)
printf("power of pointer");
else
printf("power of c");
getch();
}
Output: power of pointer
Explanation:
Here we are performing relational operation between two huge address. So first both a and b will normalize.
a=(0x5999)* (0x10)+(0x0005)=0x9990+0x0005=0x9995
b=(0x5998)* (0x10)+(0x0015)=0x9980+0x0015=0x9995
Here both huge address is representing same physical address. So a==b is true.
(Huge pointer)
(5)
#include
#include
void main()
{
int (*ptr1)();
int (*ptr2)();
void (*ptr3)();
ptr1=puts;
ptr2=getch;
ptr3=clrscr;
(*ptr3)();
(*ptr1)("POWER OF POINTER");
(*ptr2)();
Output: POWER OF POINTER
Example 2.
char *arr [3];
Ans :
arr is array of size 3 which contain address of char
program:
void main()
{
char *arr[3];
char a=1,b=2,c=3;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
clrscr();
printf("%u %u",&b,arr[1]);
printf("\n%d",*arr[2]);
getch();
}
Output :
any addresss same address
3
Example 3
char (*ptr)[5];
Ans:
ptr is pointer to array of size 5 which contain char data type.
Program:
#include
#include
void main()
{
char arr[5]={1,2,3,4,5};
char (*ptr)[5];
ptr=&arr;
ptr=(*ptr)+2;
clrscr();
printf("%d",**ptr);
getch();
}
Output: 3
Example 4
unsigned long int ( * avg ())[ 3]
Program:
Ans:
avg is such function which return type is address of array of size of 3 which contain unsigned long int data type.
Program:
#include
#include
unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
clrscr();
printf("%d",*(*ptr+2));
getch();
}
Output :3
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment