C-Interview Questions - Update 1
C-Interview Questions - Update 1
• What will be output if you will compile and execute the following c
code?
#include <stdio.h>
struct marks{
int p:3;
int c:3;
int m:2;
};
int main()
{ struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
return 0;
}
(a) 2 -6 5 (b) 2 -6 1 (c) 2 2 1 (d) Compiler error (e) None of these
Explanation
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• int huge*p=(int huge*)0XC0563331;
• int huge*q=(int huge*)0xC2551341;
• *p=200;
• printf("%d",*q); return 0;
• }
• (a)0 (b)Garbage value (c)null (d) 200 (e)Compiler
error
Explanation
• Physical address of huge pointer p Huge address: 0XC0563331
• Offset address: 0x3331
• Segment address: 0XC056
• Physical address= Segment address * 0X10 + Offset address =
• 0XC056 * 0X10 +0X3331 =0XC0560 + 0X3331 =0XC3891
• Physical address of huge pointer q
• Huge address: 0XC2551341
• Offset address: 0x1341
• Segment address: 0XC255
• Physical address= Segment address * 0X10 + Offset address
=0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891
• Since both huge pointers p and q are pointing same physical
address so content of q will also same as content of q.
• What will be output if you will compile and execute
the following c code?
• #include <stdio.h>
• int main()
• { int i;
• float a=5.2;
• char *ptr;
• ptr=(char *)&a;
• for(i=0;i<=3;i++)
• printf("%d ",*ptr++);
• return 0;
• } 01000000 10100110 01100110 01100110
Explanation
• In c float data type is four byte data type while char
pointer ptr can point one byte of memory at a time.
Memory representation of float a=5.2
• Fourth byte :01100110
• Third byte: 01100110
• Second byte:10100110
• First byte:01000000
• ptr pointer will point first fourth byte then third and so
on..
• Ans: 102 102 -90 64
• What will be output if you will compile and execute the
following c code?
• #include <stdio.h>
• int main()
• { int i;
• double a=5.2;
• char *ptr;
• ptr=(char *)&a;
• for(i=0;i<=7;i++)
• printf("%d ",*ptr++); return 0; } 01000000 00010100
11001100 11001100
11001100 11001100 11001100 11001101
• What will be output if you will compile and execute the following
c code?
• #include <stdio.h>
• int main()
• {
• printf("%s","c" "question" "bank");
• return 0;
• }
• (a) c question bank
• (b) c
• (c) bank
• (d) cquestionbank
• (e) Compiler error
Explanation
• In c string constant “xy” is same as “x” “y”
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• char *str="c-pointer";
• printf("%*.*s",10,7,str); return 0;
• }
• int main()
• {
• int a=-12; a=a>>3;
• printf("%d",a); return 0;
• }
• (a) -4 (b) -3 (c) -2 (d) -96 (e) Compiler error