Here is a listing of C language programming Objective Questions on “Data Types and Sizes” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
float f1 = 0.1;
-
if (f1 == 0.1)
-
printf("equaln");
-
else
-
printf("not equaln");
-
}
a) equal
b) not equal
c) output depends on the compiler
d) error
Answer: b
Clarification: 0.1 by default is of type double which has different representation than float resulting in inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
float f1 = 0.1;
-
if (f1 == 0.1f)
-
printf("equaln");
-
else
-
printf("not equaln");
-
}
a) equal
b) not equal
c) output depends on compiler
d) error
Answer: a
Clarification: 0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What will be the output of the following C code on a 32-bit machine?
-
#include
-
int main()
-
{
-
int x = 10000;
-
double y = 56;
-
int *p = &x;
-
double *q = &y;
-
printf("p and q are %d and %d", sizeof(p), sizeof(q));
-
return 0;
-
}
a) p and q are 4 and 4
b) p and q are 4 and 8
c) compiler error
d) p and q are 2 and 8
Answer: a
Clarification: Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
4. Which is correct with respect to the size of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
Answer: c
Clarification: char has less bytes than int and int has less bytes than double in any system
5. What will be the output of the following C code on a 64 bit machine?
-
#include
-
union Sti
-
{
-
int nu;
-
char m;
-
};
-
int main()
-
{
-
union Sti s;
-
printf("%d", sizeof(s));
-
return 0;
-
}
a) 8
b) 5
c) 9
d) 4
Answer: d
Clarification: Since the size of a union is the size of its maximum data type, here int is the largest data type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
float x = 'a';
-
printf("%f", x);
-
return 0;
-
}
a) a
b) run time error
c) a.0000000
d) 97.000000
Answer: d
Clarification: Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000
7. Which of the data types has the size that is variable?
a) int
b) struct
c) float
d) double
Answer: b
Clarification: Since the size of the structure depends on its fields, it has a variable size.