Initializing with OL

Discussion in 'C Programming' started by jimward2@gmail.com, Feb 10, 2009.

  1. Guest

    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.
    , Feb 10, 2009
    #1
    1. Advertising

  2. user923005 Guest

    On Feb 10, 12:53 pm, 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. */
    user923005, Feb 10, 2009
    #2
    1. Advertising

  3. 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) <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"
    Keith Thompson, Feb 10, 2009
    #3
  4. MisterE Guest

    "Keith Thompson" <> wrote in message
    news:...
    > 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?
    MisterE, Feb 12, 2009
    #4
  5. "MisterE" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> 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) <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"
    Keith Thompson, Feb 13, 2009
    #5
  6. CBFalconer Guest

    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>
    Try the download section.
    CBFalconer, Feb 13, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rickman
    Replies:
    12
    Views:
    6,885
    vkmselva
    Aug 29, 2007
  2. mg
    Replies:
    0
    Views:
    948
  3. moid
    Replies:
    2
    Views:
    1,970
  4. Adam Smith
    Replies:
    0
    Views:
    476
    Adam Smith
    Apr 15, 2004
  5. Hiren Patel
    Replies:
    2
    Views:
    444
    Hiren Patel
    Feb 1, 2005
Loading...

Share This Page