Initializing with OL

J

jimward2

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.
 
U

user923005

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. */
 
K

Keith Thompson

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.)
 
M

MisterE

Keith Thompson said:
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?
 
K

Keith Thompson

MisterE said:
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.)
 
C

CBFalconer

Keith said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top