12

In all of the C++ style guides I have read, I never have seen any information about numerical literal suffixes (i.e. 3.14f, 0L, etc.).

Questions

  1. Is there any style guide out there that talks about there usage, or is there a general convention?

  2. I occasionally encounter the f suffix in graphics programming. Is there any trend on there usage in the type of programming domain?

animuson
  • 53,861
  • 28
  • 137
  • 147
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • I'm not quite sure what your questions are. Are you asking about the *meaning* of the numeric literal suffixes, or are you asking about whether people have established some conventions around their use? – Greg Hewgill May 04 '12 at 23:25
  • @GregHewgill: I'm asking about whether people have established conventions on their use. – Jesse Good May 04 '12 at 23:26
  • 5
    You use them when you need to use them. Do you need a `float` constant? Use an `f` suffix. Do you need an `unsigned` constant? Use a `u` constant. And so on. – James McNellis May 04 '12 at 23:27

5 Answers5

10

The only established convention (somewhat established, anyway) of which I'm aware is to always use L rather than l, to avoid its being mistaken for a 1. Beyond that, it's pretty much a matter of using what you need when you need it.

Also note that C++ 11 allows user-defined literals with user-defined suffixes.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Good answer, I was looking for a rationale to prefer the uppercase version. – nowox Aug 21 '17 at 11:28
  • Well, I have been using the lower case version for years, and never had one confusion about this, or heard of anyone being confused by this.. maybe depends on the font used.. My counter argument to use lowercase would be: it is less typing (holding shift). I know that reading code is more important, but if it doesn't seem to matter for reading in my (limited) experience, then this is a valid argument. I'm also a long time RSI patient so things like holding shift matter to me. – Emile Vrijdags Mar 03 '21 at 08:24
5

There is no general style guide that I've found. I use capital letters and I'm picky about using F for float literals and L for long double. I also use the appropriate suffixes for integral literals.

I assume you know what these suffixes mean: 3.14F is a float literal, 12.345 is a double literal, 6.6666L is a long double literal.

For integers: U is unsigned, L is long, LL is long long. Order between U and the Ls doesn't matter but I always put UL because I declare such variables unsigned long for example.

If you assign a variable of one type a literal of another type, or supply a numeric literal of one type for function argument of another type a cast must happen. Using the proper suffix avoids this and is useful along the same lines as static_cast is useful for calling out casts. Consistent usage of numeric literal suffixes is good style and avoids numeric surprises.

People differ on whether lower or upper case is best. Pick a style that looks good to you and be consistent.

emsr
  • 15,539
  • 6
  • 49
  • 62
  • 1
    @bobobobo I think so... Either that or 0ull. I tend to prefer upper case because 'l' looks like '1'. Using suffixes is especially important with C++11 auto. – emsr Dec 17 '13 at 12:24
4

The CERT C Coding Standard recommends to use uppercase letters:

DCL16-C. Use "L," not "l," to indicate a long value

Lowercase letter l (ell) can easily be confused with the digit 1 (one). This can be particularly confusing when indicating that an integer literal constant is a long value. This recommendation is similar to DCL02-C. Use visually distinct identifiers.

Likewise, you should use uppercase LL rather than lowercase ll when indicating that an integer literal constant is a long long value.

sergej
  • 17,147
  • 6
  • 52
  • 89
0

MISRA C++ 2008 for the C++03 language states in rule M2-13-3 (at least, as cited by this Autosar document) that

A “U” suffix shall be applied to all octal or hexadecimal integer literals of unsigned type.

The linked document also compares to JSF-AV 2005 and HIC++v4.0, all these four standards require the suffixes to be uppercase.

Nevertheless I can't find a rule (but I don't have a hardcopy of MISRA C++ at hand) that states that the suffixes shall be used whenever needed. However, IIRC there is one in MISRA C++ (or maybe was it just my former company coding guidelines…)

Vser
  • 578
  • 4
  • 18
-3

Web search for "c++ numeric suffixes" returns:

http://cpp.comsci.us/etymology/literals.html

http://www.cplusplus.com/forum/general/27226/

http://bytes.com/topic/c/answers/758563-numeric-constants

Are these what you're looking for?

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • I was looking for an authoritative source, such as the Google C++ style guide, etc. that has an established convention for their use. You can Google all you want, but you won't find anything because it seems that such a thing doesn't exist. – Jesse Good May 05 '12 at 09:45