C-Interview Questions - update 2
C-Interview Questions - update 2
First byte
0 1 0 0 0 0 0 0
101000000
Ptr
• What will be output if you will compile and
execute the following c code?
void main()
{
char c=125; // 0 to 255
c=c+10;
printf("%d",c);
}
Char- Range
0
-1 1
-2 2
Char
: :
-127 126
-128 127
• What will be output if you will compile and execute the
following c code?
void main()
{
long double a = 5.45 //10 bytes sizeof()
float a=5.2; // 4 byes 8 bytes
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
}
Explanation
• 5.2 is double constant in c. In c size of double data is 8
byte while a
• is float variable. Size of float variable is 4 byte.
• So double constant 5.2 is stored in memory as:
• 101.00 11001100 11001100 11001100 11001100
11001100 11001101
• Content of variable a will store in the memory as:
• 101.00110 01100110 01100110
• It is clear variable a is less than double constant 5.2
• Since 5.2 is recurring float number so it different for
float and double.
What will be output if you will compile and execute the
following c code?
void main()
{
printf("%d",sizeof(5.2));
}
a.2,
b.4,
c.8,
d.10,
e.Error
What will be output if you will compile and
execute the following c code?
#include "stdio.h"
#include "string.h"
void main()
{
char c='\08';
printf("%d",c);
}
Explanation
#include <stdio.h>
struct marks{
int p:3; //2 bytes
int c:3;
int m:2;
};
int main()
{ p c m
struct marks s={2,-6,2}; //00000010 11111010 00000101
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
• Binary value of 2: 00000010 (Select three two
bit)
• Binary value of 6: 00000110
• Binary value of -6: 11111001+1=11111010
• (Select last three bit)
• Binary value of 5: 00000101 (Select last two bit)
Complete memory representation:
• 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; // 0xc3891
int huge*q=(int huge*)0xC2551341; // 0xc3891
*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
64 102 102
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 : 01000000
• Third byte:10100110
• Second byte: 01100110
• First byte: 01100110
• ptr pointer will point first fourth byte then third and so on..
• Ans: 102 102 -90 64
• 01000000 10100110 01100110 01100110
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 is error in following declaration?
struct outer
{
int a;
struct inner
{
char c;
};
};
(A)Nesting of structure is not allowed in c.
(B) It is necessary to initialize the member variable.
(C) Inner structure must have name.
(D) Outer structure must have name.
(E) There is not any error.
• (A)Nesting of structure is not allowed in c.
• (B) It is necessary to initialize the member
variable.
• (C) Inner structure must have name.
• (D) Outer structure must have name.
• (E) There is not any error.
Explanation
It is necessary to assign name of inner structure at the
time of declaration other wise we cannot access the
member of inner structure.
So correct declaration is:
struct outer{
int a;
struct inner{
char c;
}name;
};
• 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