On a typical platform where sizeof(float)=4, sizeof(double)=8, and sizeof(long double)=12, what does this program print?\n\n#include<stdio.h>\n#include<math.h>\nint main()\n{\n printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));\n return 0;\n}\n\nAssume long double uses extended precision storage occupying 12 bytes.

Difficulty: Medium

Correct Answer: 4, 8, 12

Explanation:


Introduction / Context:
This question focuses on literal suffixes and type sizes. In C, 3.14f is float, 3.14 is double, and 3.14l (or 3.14L) is long double. The exact size of long double is implementation-defined.



Given Data / Assumptions:

  • Assumed platform: float=4 bytes, double=8 bytes, long double=12 bytes (common on some 32-bit x86 ABIs using 80-bit extended precision stored in 12 bytes).
  • printf uses %d; sizeof yields type size in bytes as size_t but will fit typical %d for these small values in practice.


Concept / Approach:
Map each literal to its type via the suffix and then to its size: f → float, none → double, l/L → long double.



Step-by-Step Solution:
sizeof(3.14f) → sizeof(float) → 4.sizeof(3.14) → sizeof(double) → 8.sizeof(3.14l) → sizeof(long double) → 12 on the assumed platform.



Verification / Alternative check:
On other platforms (notably many 64-bit Linux systems), long double can be 16 bytes. On MSVC, long double may equal double (8 bytes). The question specifies the 12-byte assumption to avoid ambiguity.



Why Other Options Are Wrong:
4,4,4 and 4,8,8 ignore the long double difference.4,8,10 is not a typical size tuple for standard ABIs.



Common Pitfalls:
Forgetting literal suffixes or assuming sizes are universal across compilers and architectures.



Final Answer:
4, 8, 12

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion