Open In App

Output of C programs | Set 63

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Structure padding, integer promotion and sequence points
Q1. Consider the following code: 
 

C




#include<stdio.h>
struct geeks{
  int i;
  char c;
} obj;
 
int main()
{
  printf("%ld", sizeof(obj));
}


What would be the output of the above code? 
A.
B.
C.
D. Can’t be determined 
 

Output: Can't be determined

Explanation: 
It’s all about structure padding. C compiler knows that storing unaligned data in RAM may be costly, so it pads data according to the requirement. If there is 5 bytes of data in a structure, it will probably make it 8 or 16 or 6. Or whatever it wants. There are extensions like GCC attributes aligned and packed that allow some control over this process, but they are non-standard. C itself does not define padding attributes. So the right answer is ‘Can’t be determined’.
Q2. Consider the following code: 
 

C




#include<stdio.h>
 
int main(){
 
  char a = 0;
  short int b = 0;
  printf("%d", sizeof(b) == sizeof(a+b));
  return 0;
}


What would be the output of the above code? 
A.
B.
C.
D. Can’t be determined 
 

Output: Can't be determined

Explanation: It’s all about integer promotion. Data types like ‘char’, ‘short int’ are automatically promoted to ‘int’ when a calculation is performed on them. But even so, the expression (sizeof(b) == sizeof(a+b)) compare sizes not types and according to C standard, it’s guarantee that the size of ‘short int’ can’t be greater than size of ‘int’. Thus we can’t assure about what will ‘sizeof(a+b)’ will return therefore correct answer is ‘Can’t be determined’.
Q3. Consider the following code: 
 

C




#include<stdio.h>
 
int main()
{
  char a = ' ' * 13;
  printf("%d", a);
  return 0;
}


What would be the output of the above code ? 
A. 416 
B. 160 
C. -96 
D. Can’t be determined 
 

Output: Can't be determined

Explanation: 
ASCII value of whitespace character(‘ ‘) is 32, Firstly the expression (‘ ‘ * 13) will not integer overflows(because of integer promotion), thus behaviour is undefined. Secondly ‘char type'(signed or unsigned) is not defined by standard, thus it will be implementation specific. 
But even more, the size of the char type itself is not specified in bits either. There were platforms where it was 6 bits (trigraphs), and there are platforms where all five integer types are 32 bits. Without all this details specified, every speculation about the result is invalid, so the answer is: “Can’t be determined”.
Q4. Consider the following code: 
 

C




#include<stdio.h>
 
int main()
{
  int i = 16;
  printf("%d", (((i >= i) << i) >> i) <= i);
  return 0;
}


What would be the output of the above code? 
A.
B.
C. 16 
D. Can’t be determined 
 

Output: Can't be determined

Explanation: 
Output of above expression is compiler dependent, as the size of ‘int’ is not directly specified in C standard. It can easily be 16 bits, then the very first operation after comparison((i >= i) << i) will cause the over-shift and that’s plain undefined behavior. It’s not C fault, on some platforms it is even undefined in assembly, so the compiler simply can’t give the valid guarantees about the above expression. Hence the answer will be ‘Can’t be determined’.
Q5. Consider the following code: 
 

C




#include<stdio.h>
 
int main()
{
  int i = 0;
  int ans = i++ + ++i;
  printf("%d ", ans);
  return 0;
}


What would be the output of the above code? 
A.
B.
C.
D. Can’t be determined 
 

Output: Can't be determined

Explanation: 
In C language, the expression “i++ + ++i” has no meaning as it violate the rule that same variable can’t be changed more than once without sequence point. Sequence points are basically a point in code where the compiler guarantees to have finished off all evaluations. For instance, a semi-colon at the end of a statement is a sequence point
In this expression, that value of variable ‘i’ is changing twice without sequence point. Therefore compiler can do anything it wants and will lead to an undefined behaviour. Hence the answer will be ‘Can’t be determined’.
References: https://hackernoon.com/so-you-think-you-know-c-8d4e2cd6f6a6
 



Last Updated : 13 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads