Converting long long to char

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

    Converting long long to char

    Hi,

    I've need a function to print a long long to the console and needs to
    run under Win32 and some unknown variant of *Nix (a client's
    unspecified server)

    in Win32 I've got

    _int64 iVal
    sprintf(cTemp, "Wrong length %I64d\n", iVal);

    The control code (and the _int 64 type) aren't standard C


    I'd rather use a runtime function that write m own...

    Ta.
  • jacob navia

    #2
    Re: Converting long long to char

    silangdon wrote:[color=blue]
    > Hi,
    >
    > I've need a function to print a long long to the console and needs to
    > run under Win32 and some unknown variant of *Nix (a client's
    > unspecified server)
    >
    > in Win32 I've got
    >
    > _int64 iVal
    > sprintf(cTemp, "Wrong length %I64d\n", iVal);
    >
    > The control code (and the _int 64 type) aren't standard C
    >
    >
    > I'd rather use a runtime function that write m own...
    >
    > Ta.[/color]

    In standard C you can write:

    long long iVal;
    sprintf(cTemp," Wrong length %lld\n",iVal);

    long long is a standard C99 type, and its format is %lld

    Comment

    • Ben Pfaff

      #3
      Re: Converting long long to char

      silangdon <me@you.twang > writes:
      [color=blue]
      > I've need a function to print a long long to the console and needs to
      > run under Win32 and some unknown variant of *Nix (a client's
      > unspecified server)[/color]

      The standard length modifier for long long is `ll', as in `%lld'.
      --
      "To get the best out of this book, I strongly recommend that you read it."
      --Richard Heathfield

      Comment

      • David Resnick

        #4
        Re: Converting long long to char

        silangdon wrote:
        [color=blue]
        > Hi,
        >
        > I've need a function to print a long long to the console and needs to
        > run under Win32 and some unknown variant of *Nix (a client's
        > unspecified server)
        >
        > in Win32 I've got
        >
        > _int64 iVal
        > sprintf(cTemp, "Wrong length %I64d\n", iVal);
        >
        > The control code (and the _int 64 type) aren't standard C
        >
        >
        > I'd rather use a runtime function that write m own...
        >
        > Ta.[/color]

        You can do the following if you need to for all platforms you support.
        Details for any specific platform are off topic here, but here
        is an example:

        #if defined(_WIN32) /* Or some other MSVC specific define */
        #define my_longlong _int64
        #define PRINTF_LL "%I64d"
        #else
        #define my_longlong long long int
        #define PRINTF_LL "%lld"
        #endif

        my_longlong iVal;
        sprintf(cTemp, "Wrong length " PRINTF_LL "\n", iVal);

        -David

        Comment

        • Keith Thompson

          #5
          Re: Converting long long to char

          jacob navia <jacob@jacob.re mcomp.fr> writes:[color=blue]
          > silangdon wrote:[color=green]
          >> Hi, I've need a function to print a long long to the console and
          >> needs to
          >> run under Win32 and some unknown variant of *Nix (a client's
          >> unspecified server)
          >> in Win32 I've got _int64 iVal
          >> sprintf(cTemp, "Wrong length %I64d\n", iVal);
          >> The control code (and the _int 64 type) aren't standard C I'd rather
          >> use a runtime function that write m own...
          >> Ta.[/color]
          >
          > In standard C you can write:
          >
          > long long iVal;
          > sprintf(cTemp," Wrong length %lld\n",iVal);
          >
          > long long is a standard C99 type, and its format is %lld[/color]

          Yes, but it's entirely possible that the OP needs to deal with one or
          more implementations that don't support long long and/or the "%lld"
          format. (I've seen systems that have one but not the other.)

          C99 is the official standard, but pretending it's universal in the
          real world doesn't make it so, and doesn't help people who need to
          deal with pre-C99 implementations .

          Certainly you should use long long and "%lld" on systems that support
          them (and even many pre-C99 implementations do so), but there's still
          often a need to use non-standard extensions to C90 to achieve the same
          result.

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

          • jacob navia

            #6
            Re: Converting long long to char

            Sorry Keith but how would you implement long long in C89???

            I mean, after having implemented it in lcc-win32 this is not
            really easy :-)

            If he hasn't long long then he is stuck. But he was speaking of
            *nix system, and in most of them gcc is available, so I think your
            fears aren't sustained by any real data. It would be surprising to find
            a unix system without gcc or long long.

            jacob

            Comment

            • tigervamp

              #7
              Re: Converting long long to char

              jacob navia wrote:[color=blue]
              > Sorry Keith but how would you implement long long in C89???
              >
              > I mean, after having implemented it in lcc-win32 this is not
              > really easy :-)[/color]

              Just because it's not easy for you doesn't mean it hasn't been done by
              others.
              [color=blue]
              > If he hasn't long long then he is stuck. But he was speaking of
              > *nix system, and in most of them gcc is available,[/color]

              He also said it needed to run on Windows which has a long long int
              (they actually call it __int64 or something like that) but does not
              support the "lld" format specifier. Windows does not support C99 and
              doesn't seem to have any plans to do so.
              [color=blue]
              > so I think your
              > fears aren't sustained by any real data. It would be surprising to[/color]
              find[color=blue]
              > a unix system without gcc or long long.[/color]

              Just because gcc is available (which it very well might not be) doesn't
              mean that the developer will have the luxury of using it (he indicated
              that the machine was client provided).
              [color=blue]
              > jacob[/color]

              Comment

              • silangdon

                #8
                Re: Converting long long to char

                On Mon, 21 Mar 2005 12:27:08 -0500, David Resnick
                <dresnick@N.o.S .p.A.m.p.l.e.a. s.e.alum.mit.ed u> wrote:

                [color=blue]
                >
                >You can do the following if you need to for all platforms you support.
                >Details for any specific platform are off topic here, but here
                >is an example:
                >
                >#if defined(_WIN32) /* Or some other MSVC specific define */
                >#define my_longlong _int64
                >#define PRINTF_LL "%I64d"
                >#else
                >#define my_longlong long long int
                >#define PRINTF_LL "%lld"
                >#endif
                >
                >my_longlong iVal;
                >sprintf(cTem p, "Wrong length " PRINTF_LL "\n", iVal);
                >
                >-David[/color]


                Thanks David & Others

                - I do have a few #ifdef for long long / _int64 specific bits in my
                code. Thanks for the format codes

                As for what version of C their version of *nix supports, they are
                pretty up to date so if I can compile it under whatever linux or unix
                or win32 platforms I can find I'll be satisfied.


                Comment

                • Richard Bos

                  #9
                  Re: Converting long long to char

                  "tigervamp" <rgamble99@gmai l.com> wrote:
                  [color=blue]
                  > jacob navia wrote:[color=green]
                  > > Sorry Keith but how would you implement long long in C89???
                  > >
                  > > I mean, after having implemented it in lcc-win32 this is not
                  > > really easy :-)[/color]
                  >
                  > Just because it's not easy for you doesn't mean it hasn't been done by
                  > others.[/color]

                  *Smothers self*
                  [color=blue][color=green]
                  > > If he hasn't long long then he is stuck. But he was speaking of
                  > > *nix system, and in most of them gcc is available,[/color]
                  >
                  > He also said it needed to run on Windows which has a long long int
                  > (they actually call it __int64 or something like that) but does not
                  > support the "lld" format specifier. Windows does not support C99 and
                  > doesn't seem to have any plans to do so.[/color]

                  YM Microsoft doesn't support C99. This need not prevent other
                  implementors from providing C99 support on MS Windows. I haven't tried
                  Pelles C extensively, but its help file does describe C99, so it
                  probably does support a good sized percentage at least.

                  Richard

                  Comment

                  Working...