Is there a C ++ style guide that talks about numeric suffixes of a literal?

In all the C ++ style manuals that I read, I never saw any information about the suffixes of numeric literals (i.e. 3.14f , 3.14f , etc.).

Questions

  • Are there any style guides where use is mentioned, or is there a general agreement?

  • I sometimes encounter the suffix f in graphical programming. Is there any tendency to use programming domain type?

+8
c ++ coding-style literals
source share
4 answers

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

I assume that 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 . The order between U and L does not matter, but I always set UL , because I declare such variables as unsigned long for example.

If you assign a literal of one type to a literal of another type or specify a numeric literal of one type for an argument to a function of another type, an act must occur. Using the proper suffix avoids this and is useful on the same lines as static_cast, useful for calling throws. Consistent use of numeric letter suffixes is a good style and avoids numeric surprises.

People differ in whether lower or upper case is better. Choose a style that looks good to you and is consistent.

+4
source share

The only established agreement (to some extent established, in any case) that I know of is to always use L , not L , to avoid being mistaken for 1 . Other than that, it pretty much depends on what you need, when you need it.

Also note that C ++ 11 allows you to define custom literals with user-defined suffixes.

+8
source share

The CERT C encoding standard recommends capitalization:

DCL16-C. Use "L" rather than "l" to indicate a long value

The lowercase letter l (ell) is easy to confuse with the number 1 (one). This can be especially confusing if you indicate that the integer constant is long. This recommendation is similar to DCL02-C. Use visually different identifiers .

Similarly, you should use upper case LL instead of lowercase ll, indicating that the integer literal constant is a long long value.

+3
source share
-one
source share

All Articles