Velocity Reviews > Initializing with OL

# Initializing with OL

jimward2@gmail.com
Guest
Posts: n/a

 02-10-2009
Is there a difference initializing using:

long a = 0;

and initializing using

long a = 0L;

I can't find this discussed in any of my reference books.

user923005
Guest
Posts: n/a

 02-10-2009
On Feb 10, 12:53*pm, (E-Mail Removed) wrote:
> Is there a difference initializing using:
>
> long a = 0;
>
> and initializing using
>
> long a = 0L;
>
> I can't find this discussed in any of my reference books.

People use the second form to shut up over-zealous C parsers.
It's not superior to the first form.
But if you like a nice, quiet compile, go ahead and qualify the
constant with the exact type.

In some sense it makes sense to make specific assignments like that
because real error or warning messages can be harder to pick out if
you have 10,000 spurious warnings filling up your screen.

You'll see this even more often:

float f = 2.5f;

which (to me) makes very little sense for a constant that is not near
the edges of the type's dynamic range, though this is a very important
warning:

float f;
double d;
d = my_calculation();
f = d; /* I don't mind the warning here at all. It's useful. */

Keith Thompson
Guest
Posts: n/a

 02-10-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> Is there a difference initializing using:
>
> long a = 0;
>
> and initializing using
>
> long a = 0L;
>
> I can't find this discussed in any of my reference books.

In effect, there's no difference.

0 is of type int. The initialization causes this int value to be
converted to type long. 0L is of type long, so no conversion is
necessary. But both have exactly the same effect: storing the value
zero in a.

(It's conceivable that a naive compiler might generate different code
for the two initializations, but realistically it's almost certain to
be the same in both cases.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

MisterE
Guest
Posts: n/a

 02-12-2009

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) writes:
>> Is there a difference initializing using:
>>
>> long a = 0;
>>
>> and initializing using
>>
>> long a = 0L;
>>
>> I can't find this discussed in any of my reference books.

>
> In effect, there's no difference.
>
> 0 is of type int. The initialization causes this int value to be
> converted to type long. 0L is of type long, so no conversion is
> necessary. But both have exactly the same effect: storing the value
> zero in a.
>
> (It's conceivable that a naive compiler might generate different code
> for the two initializations, but realistically it's almost certain to
> be the same in both cases.)

If int is 2 bytes in size and long is 4 bytes in size should there be a
warning or error for:

long a = 70000;

Is the compiler meant to truncate 70000 to 16bit integer then assigned to
long?

Keith Thompson
Guest
Posts: n/a

 02-13-2009
"MisterE" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> (E-Mail Removed) writes:
>>> Is there a difference initializing using:
>>>
>>> long a = 0;
>>>
>>> and initializing using
>>>
>>> long a = 0L;
>>>
>>> I can't find this discussed in any of my reference books.

>>
>> In effect, there's no difference.
>>
>> 0 is of type int. The initialization causes this int value to be
>> converted to type long. 0L is of type long, so no conversion is
>> necessary. But both have exactly the same effect: storing the value
>> zero in a.
>>
>> (It's conceivable that a naive compiler might generate different code
>> for the two initializations, but realistically it's almost certain to
>> be the same in both cases.)

>
> If int is 2 bytes in size and long is 4 bytes in size should there be a
> warning or error for:
>
> long a = 70000;
>
> Is the compiler meant to truncate 70000 to 16bit integer then assigned to
> long?

No, an unadorned decimal constant is of type int only if the value is
in the range of type int. Specifically, its type is the first of these:
int
long int
long long int
in which its value will fit. So if INT_MAX < 70000, then 70000 is of
type long int.

For an octal or hexadecimal constant, the list of types is:
int
unsigned int
long int
unsigned long int
long long int
unsigned long long int

Various combinations of 'u', 'l', and 'll' suffixes (whcih can be in
upper or lower case) can change the type. The rules are in section
6.4.4.1 of the C99 standard. (The rules were a bit different in C90.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer
Guest
Posts: n/a

 02-13-2009
Keith Thompson wrote:
>

.... snip ...
>
> No, an unadorned decimal constant is of type int only if the
> value is in the range of type int. Specifically, its type is the
> first of these:
> int
> long int
> long long int
> in which its value will fit. So if INT_MAX < 70000, then 70000 is
> of type long int.
>
> For an octal or hexadecimal constant, the list of types is:
> int
> unsigned int
> long int
> unsigned long int
> long long int
> unsigned long long int
>
> Various combinations of 'u', 'l', and 'll' suffixes (whcih can be
> in upper or lower case) can change the type. The rules are in
> section 6.4.4.1 of the C99 standard. (The rules were a bit
> different in C90.)

A very useful summary.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>