Output of C programs | Set 63

2

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

#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. 4
B. 5
C. 8
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:

#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. 0
B. 1
C. 2
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:

#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 charater(‘ ‘) 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:

#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. 0
B. 1
C. 16
D. Can’t be determined

Output: Can't be determined

Explanation:
Output of above expresison 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 comparision((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:

#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. 1
B. 2
C. 3
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

This article is contributed by Shubham Bansal 13. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.