ty452567
ty452567

Reputation: 61

C++ suffix L vs specifying long double

Question What does the "L" mean at the end of an integer literal? describes what L means at the end of int literal. I am confused why not to specify long int i = 33 instead of int i = 33L? The same thing applies to other types. Instead of specifying double b = 3.0L why not to write long double?

Upvotes: 1

Views: 846

Answers (2)

Slava
Slava

Reputation: 44278

I am confused why not to specify long int i = 33 instead of int i = 33L?

Because that 2 statements mean 2 different things:

long int i = 33; // take constant of type int, convert it to long int and assign to i.

int i = 33L; // take constant of type long int, convert it to int and assign to i.

So you end up with variable i of different type. Same for the double in your example.

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234875

Although in many instances, you can rely on the extremely well-defined widening conversion rules, sometimes you need to be explicit about the type of a literal.

For example

long double f = 1.0;
f = std::max(f, 2.0L);

does not compile unless the arguments of max have the same type. Writing double b = 3.0L would indeed be pointless, and a compiler will almost certainly ignore you and compile double b = 3.0.

Upvotes: 1

Related Questions