long long data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dijaster

    long long data type

    Recently I have had the need to run loops of at least 10^10 steps.
    Since 32 bit integers apparently can only store values up to about
    2*10^9, I have tried to use the "long long" data type. However, it has
    not worked and I am unsure why. For example, I try the following code:

    testlong.c:

    #include <stdio.h>
    #include <stdlib.h>

    int main() {
    long long a;
    a = 10000000000LL; //10^10
    printf("a = %i\n", a);
    return EXIT_SUCCESS;
    }

    and compile it by:

    gcc testlong.c -o testlong

    then when I run the executable I obtain the following:

    a = 1410065408

    Does anyone know how to fix this problem? Perhaps gcc requires some
    commandline argument to support long long ints?
    Thanks for any suggestions.

  • Robert Bauck Hamar

    #2
    Re: long long data type

    dijaster wrote:[color=blue]
    > Recently I have had the need to run loops of at least 10^10 steps.
    > [...]. For example, I try the following code:
    >
    > testlong.c:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main() {
    > long long a;
    > a = 10000000000LL; //10^10
    > printf("a = %i\n", a);[/color]

    As the format string specifies, printf expects it's second argument to
    be an int.
    [color=blue]
    > return EXIT_SUCCESS;
    > }
    >
    > and compile it by:
    >
    > gcc testlong.c -o testlong
    >
    > then when I run the executable I obtain the following:
    >
    > a = 1410065408[/color]

    You need to specify the long long length modifier (ll) to printf:
    printf("%lli\n" , a);

    --
    Robert Bauck Hamar

    Comment

    • dijaster

      #3
      Re: long long data type

      Thanks Robert. That seems to have solved the problem.

      Comment

      • Keith Thompson

        #4
        Re: long long data type

        "dijaster" <dijaster@yahoo .com> writes:[color=blue]
        > Thanks Robert. That seems to have solved the problem.[/color]

        Note that there may be systems on which the compiler supports
        "long long", but the runtime library's implementation of printf()
        doesn't (or supports it in a non-standard way).

        The "long long" type was added to the language in the 1999 version of
        the standard (C99), which is not yet widely implemented. Many pre-C99
        compilers support it as an extension, but as I recall consensus on the
        name of the type was reached before it was reached on the format
        strings for printf.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Erik de Castro Lopo

          #5
          Re: long long data type

          dijaster wrote:[color=blue]
          >
          > Recently I have had the need to run loops of at least 10^10 steps.
          > Since 32 bit integers apparently can only store values up to about
          > 2*10^9, I have tried to use the "long long" data type. However, it has
          > not worked and I am unsure why. For example, I try the following code:
          >
          > testlong.c:
          >
          > #include <stdio.h>
          > #include <stdlib.h>
          >
          > int main() {
          > long long a;
          > a = 10000000000LL; //10^10
          > printf("a = %i\n", a);[/color]

          Thats the wrong conversion character. Try:

          printf("a = %llu\n", a);

          Erik
          --
          +-----------------------------------------------------------+
          Erik de Castro Lopo nospam@mega-nerd.com (Yes it's valid)
          +-----------------------------------------------------------+
          Linux, the UNIX defragmentation tool.

          Comment

          • Lawrence Kirby

            #6
            Re: long long data type

            On Mon, 20 Dec 2004 08:25:24 +1100, Erik de Castro Lopo wrote:
            [color=blue]
            > dijaster wrote:[color=green]
            >>
            >> Recently I have had the need to run loops of at least 10^10 steps.
            >> Since 32 bit integers apparently can only store values up to about
            >> 2*10^9, I have tried to use the "long long" data type. However, it has
            >> not worked and I am unsure why. For example, I try the following code:
            >>
            >> testlong.c:
            >>
            >> #include <stdio.h>
            >> #include <stdlib.h>
            >>
            >> int main() {
            >> long long a;
            >> a = 10000000000LL; //10^10
            >> printf("a = %i\n", a);[/color]
            >
            > Thats the wrong conversion character. Try:
            >
            > printf("a = %llu\n", a);[/color]

            %llu is for unsigned long long, for long long use %lld.

            You CAN also use %lli. However %d forms are usually preferred over %i
            because %d was the original, %i was only a later addition; it is possible
            that some old compilers may not support %i, and %d is what C programmers
            tend to be used to. d can be easier to read than i in some character
            sets. Also remember that %d and %i do different things in scanf()
            conversions, and %d is the closer match to the printf() behaviour.

            Lawrence

            Comment

            Working...