C/C++ literals: By default, a real (floating) numeric literal such as 3.5 is treated as which type?

Difficulty: Easy

Correct Answer: double

Explanation:


Introduction / Context:
Literal types influence overload resolution, precision, and performance. In both C and C++, an unsuffixed real literal like 3.5 is assigned a default floating type unless a suffix changes it. Recognizing this default helps you pick correct function overloads and avoid unintentional narrowing conversions.


Given Data / Assumptions:

  • No suffix is present (e.g., 'f' or 'L').
  • We refer to standard language rules, not compiler extensions.
  • The literal is written in decimal floating notation.


Concept / Approach:

  • Unsuffixed floating literals are of type double.
  • Adding 'f' or 'F' yields type float; adding 'l' or 'L' yields long double (subject to implementation specifics).
  • There is no 'far double' in standard C/C++.


Step-by-Step Solution:

Identify the absence of a suffix → default applies.Default floating literal type → double.Therefore, answer: 'double'.


Verification / Alternative check:

Use sizeof(3.5) versus sizeof(3.5f) in C++: commonly 8 bytes vs 4 bytes on typical platforms, confirming different default types.


Why Other Options Are Wrong:

  • float/long double require explicit suffixes or contexts.
  • far double: Not a standard type.


Common Pitfalls:

  • Accidentally calling 'float' overloads with double arguments, causing extra casts or ambiguity.
  • Assuming 'long double' is always higher precision; implementation details vary.


Final Answer:

double

More Questions from Declarations and Initializations

Discussion & Comments

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