C Programming Questions and Answers – Data Types and Sizes
1. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
Explanation: 65000 comes in the range of short (16-bit) which occupies the least memory. Signed short ranges from -32768 to 32767 and hence we should use unsigned short.
2. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
Explanation: typedef and struct are used to define user-defined data types.
3. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
Explanation: The size of the data types depend on the system.
4. What will be the output of the following C code?
-
#include <stdio.h>
-
int main()
-
{
-
signed char chr;
-
chr = 128;
-
printf("%d\n", chr);
-
return 0;
-
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
Explanation: signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128
5. What will be the output of the following C code?
-
#include <stdio.h>
-
int main()
-
{
-
char c;
-
int i = 0;
-
FILE *file;
-
file = fopen("test.txt", "w+");
-
fprintf(file, "%c", 'a');
-
fprintf(file, "%c", -1);
-
fprintf(file, "%c", 'b');
-
fclose(file);
-
file = fopen("test.txt", "r");
-
while ((c = fgetc(file)) != -1)
-
printf("%c", c);
-
return 0;
-
}
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
Explanation: None.
Output:
$ cc pgm3.c
$ a.out
a
6. What is short int in C programming?
a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned
Explanation: None.
7. What will be the output of the following C code?
-
#include <stdio.h>
-
int main()
-
{
-
int a[5] = {1, 2, 3, 4, 5};
-
int i;
-
for (i = 0; i < 5; i++)
-
if ((char)a[i] == '5')
-
printf("%d\n", a[i]);
-
else
-
printf("FAIL\n");
-
}
a) The compiler will flag an error
b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
Explanation: The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAIL
FAIL
FAIL
FAIL
FAIL
8. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
Explanation: Both %d and %i can be used as a format identifier for int data type.
A data type is a specific type of information. For example, if the value “1.25” is assigned to the variable “var1,” the variable will be formed as a floating point data type. The variable would be assigned a string data type if it was set to “Hello world!” A real data type is a data type that is used in a computer application to represent a real number’s approximation. Because real numbers are not countable, computers can’t accurately represent them with a finite quantity of data. A computer will almost always employ a rational approximation to a real number.