Sunday, January 4, 2009

C Program Coding Question With Explanation


What will be output:
(1)
void main()
{
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
clrscr();
*p=25;
(*p)++;
printf("%d",*q);
getch();
}
Output: 26
Explanation: 
Far address of p and q are representing same physical address . 
Physical address of 0x55550005= 0x5555*ox10+ox0005= 0x55555
Physical address of 0x53332225=0x5333*0x10+ox2225=0x55555
*p =25, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 26
*q also means content at memory location 0x55555 which is 26
(2)
void main()
{
char i=13;
clrscr();
while(i)
{
i++;
}
printf("%d",i);
getch();
}
Output: 0
Explanation: 
while( i) will true if i non negative .
while (i) will false only when i=0.
Since i is char type of data which is cyclic in nature.


i will get maximum value to 127 the minimum value -128 then it will be zero .When i=0 ,while condition will false and i will come outside the loop and at that time value of I is zero. 
(3) Write a c program without using == operator compare two numbers?
Ans:
void main()
{
int a=5,b=5;
clrscr();
if(b^a)
printf("EQUAL”);
getch();
}
Output: EQUAL
(4)
void main()
{
char a='\x82';
clrscr();
printf("%d",a);
getch();
}
Output:-126

Explanation: \x symbol indicate the hexadecimal number system in character constant. So 82 is hexadecimal number its equivalent binary value is 130. 130 is beyond the range of signed char. Since signed char is cyclic in nature so its value will be
130-256=-126
(For more detail visit the link data type)
(5) 
void main()
{
enum {a,b=32766,c,d,e=0};
clrscr();
printf("%d",a);
getch();
}
Output: error
Explanation: value of enum constant must be within range of signed int .Maximum value of signed int is 32767 while value of d=32768
(6)
#define char double
void main()
{
char a=9;
clrscr();
printf("%d",sizeof(a));
getch();
}
Output: 8

Explanation: #define is preprocessor directive which process before the actual compilation. #define paste value in the in place of macro in the program before actual compilation. New source code will be:
void main()
{
double a=9;
clrscr();
printf("%d",sizeof(a));
getch();
}
Now a is double type of data which size is 8 byte.
(7)
How you will copy the content of one array to another in another array by one line statement?
Ans:
void main()
{
int i;
struct arr1
{
int a[8];
} ;
struct arr1 p={1,2,3,4,5,6,7,8},q;
q=p; //copy in one line
clrscr();
for(i=0;i<8;i++)
{
printf("%d",q.a[i]);
}
getch();
}
Output: 12345678
(8)
What will be output?
void main()
{
char arr[8]={'S','A','V','R','I','Y','A'};
char *p;
p=(char *)(arr+2)[2];
clrscr();
printf("%c",p);

getch();
}
Output: I

(9)
void main()
{
float a=5e4;
clrscr();
printf("%f",a);

getch();
}
output :50000.000000
Explanation: 5e4 is in exponential form is (5*10^4)
(10)
void main()
{
float a=5e40;
clrscr();
printf("%f",a);

getch();
}
Output: +INF

Explanation: 5e40 is beyond the range of float so output is plus infinity.
(q) What will be output?
Ans: 
int * operation(int,int);
void main()
{
int p=6,q=9,*r;
r=operation (p,q);
clrscr();
printf("%d",*r);
getch();
}
int * operation(int p,int q)
{
int a=1,b;
b=!(p)+ a * q++;
return &b;
}
Output: garbage
Explanation: Variable b is auto type. Its scope is within the operation function. When control will transfer to the operation function b will die. We are returning address of variable b to r.
*r means content at location control will transfer to the operation function b will die. We are returning address of variable b to r. When control transfer to main function assigning the address of b till now variable b has dead and r will store some garbage value.*r means content at location &r so it will show wrong output.
To find write output declare b as static data type. Scope of static data type is entire the program.

int * operation(int,int);
void main()
{
int p=6,q=9,*r;
r=operation(p,q);
clrscr();
printf("%d",*r);
getch();
}
int * operation(int p,int q)
{
int a=1;
static int=b;
b=!(p)+ a * q++;
return &b;
}
Output: 9

(q)
void main()
{
extern int p;

clrscr();
printf("%d",p);
getch();
}
int p=34;
Output: 34
Explanation: extern storage class search the value of p in outside the function. (for more detail visit the link function then storage class)
(q)
void main()
{
extern int p=23;

clrscr();
printf("%d",p);
getch();
}

Output: error
Explanation: Extern variable cannot be initialized due to for extern variable has not any storage space.
void main()
{
extern int p;

clrscr();
printf("%d",p);
getch();
}
int p;

Output: 0
Explanation: Default value of extern data type is zero.
void main()
{
int p;

p= 4>7 ? return 5:return 6;
clrscr();
printf("%d",p);
getch();
}

Output: error

Explanation: We cannot use return keyword in conditional operator.
(q)
void main()
{
int p=0;

p= 4>7 ? p=11:p=22;

clrscr();
printf("%d",p);
getch();
}

Output: error
Explanation: assignment operator = has less precedence than conditional operator. Condition 4>7 is false .So false i.e. p=12 will execute but since = has less precedence so first value of p will assign 0 because initial value of p is. Now statement is
0=22
We cannot assign a value to constant. So error will Lvalue required.
To solve such problem use ( ) operator it has higher precedence than conditional operator.
void main()
{
int p=0;

p= 4>7 ? (p=11):(p=22);

clrscr();
printf("%d",p);
getch();
}
Output: 22

(q)
void main()
{
int p=0;

p= 11>7 ? (p=11):p=22;

clrscr();
printf("%d",p);
getch();
}
Output: 11
Explanation:
Condition operator evaluates only true part 11>7 is true. So it will assign 11 to p .It doesn’t check p=22 is write or wrong. It only checks its syntax is correct or not.
(q)

No comments:

Blog List