C Data Types

Question 1
Predict the output of following program. Assume that the numbers are stored in 2's complement form.
#include<stdio.h> 
int  main() 
{ 
   unsigned int x = -1; 
   int y = ~0; 
   if (x == y) 
      printf("same"); 
   else
      printf("not same"); 
   return 0; 
}
Tick
same
Cross
not same


Question 1-Explanation: 
-1 and ~0 essentially have same bit pattern, hence x and y must be same. In the comparison, y is promoted to unsigned and compared against x (See this for promotion rules). The result is “same”. However, when interpreted as signed and unsigned their numerical values will differ. x is MAXUNIT and y is -1. Since we have %u for y also, the output will be MAXUNIT and MAXUNIT.
Question 2
Which of the following is not a valid declaration in C?
1. short int x;
2. signed short x;
3. short x;
4. unsigned short x;
Cross
3 and 4
Cross
2
Cross
1
Tick
All are valid


Question 2-Explanation: 
All are valid. First 3 mean the same thing. 4th means unsigned.
Question 3
Predict the output
#include <stdio.h>

int main()
{
   float c = 5.0;
   printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
   return 0;
}


Cross
Temperature in Fahrenheit is 41.00
Tick
Temperature in Fahrenheit is 37.00
Cross
Temperature in Fahrenheit is 0.00
Cross
Compiler Error


Question 3-Explanation: 
Since 9 and 5 are integers, integer arithmetic happens in subexpression (9/5) and we get 1 as its value. To fix the above program, we can use 9.0 instead of 9 or 5.0 instead of 5 so that floating point arithmetic happens.
Question 4
Predict the output of following C program
#include <stdio.h>
int main()
{
    char a = 012;

    printf("%d", a);

    return 0;
}
Cross
Compiler Error
Cross
12
Tick
10
Cross
Empty


Question 4-Explanation: 
The value \'\\012\' means the character with value 12 in octal, which is decimal 10. Note: It is equivalent to char a = 012 and int a = ‘\\012’ and int a = 012.
Question 5
In C, sizes of an integer and a pointer must be same.
Cross
True
Tick
False


Question 5-Explanation: 
Sizes of integer and pointer are compiler dependent. The both sizes need not be same.
Question 6
Output?
int main()
{
    void *vptr, v;
    v = 0;
    vptr = &v;
    printf("%v", *vptr);
    getchar();
    return 0;
}
Cross
0
Tick
Compiler Error
Cross
Garbage Value


Question 6-Explanation: 
void is not a valid type for declaring variables. void * is valid though.
Question 7
Assume that the size of char is 1 byte and negatives are stored in 2's complement form
#include<stdio.h>
int main()
{
    char c = 125;
    c = c+10;
    printf("%d", c);
    return 0;
}
Cross
135
Cross
+INF
Tick
-121
Cross
-8


Question 7-Explanation: 
125 is represented as 01111101 in binary and when we add 10 i.e 1010 in binary it becomes : 10000111. Now what does this number represent? Firstly, you should know that char can store numbers only -128 to 127 since the most significant bit is kept for sign bit. Therefore 10000111 represents a negative number. To check which number it represents we find the 2’s complement of it and get 01111001 which is = 121 in decimal system. Hence, the number 10000111 represents -121.
Question 8
#include <stdio.h>
int main()
{
    if (sizeof(int) > -1)
        printf("Yes");
    else
        printf("No");
    return 0;
}
Cross
Yes
Tick
No
Cross
Compiler Error
Cross
Runtime Error


Question 8-Explanation: 
In C, when an integer value is compared with an unsigned it, the int is promoted to unsigned. Negative numbers are stored in 2\'s complement form and unsigned value of the 2\'s complement form is much higher than the sizeof int.
Question 9
Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which of the following statements is most likely to set p correctly?
Cross
p = n * (n-1) * (n-2) / 6;
Tick
p = n * (n-1) / 2 * (n-2) / 3;
Cross
p = n * (n-1) / 3 * (n-2) / 2;
Cross
p = n * (n-1) * (n-2) / 6.0;


Question 9-Explanation: 
As n is large, the product n*(n-1)*(n-2) will go out of the range(overflow) and it will return a value different from what is expected. Therefore, option (A) and (D) are eliminated. So we consider a shorter product n*(n-1). n*(n-1) is always an even number. So the subexpression \" n * (n-1) / 2 \" in option B would always produce an integer, which means no precision loss in this subexpression. And when we consider \" n*(n-1)/2*(n-2) \", it will always give a number which is a multiple of 3. So dividing it with 3 won\'t have any loss.
Question 10
Output of following program?
#include<stdio.h>
int main()
{
    float x = 0.1;
    if ( x == 0.1 )
        printf("IF");
    else if (x == 0.1f)
        printf("ELSE IF");
    else
        printf("ELSE");
}

Tick
ELSE IF
Cross
IF
Cross
ELSE


There are 14 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads