Converting long long to char

Discussion in 'C Programming' started by silangdon, Mar 21, 2005.

  1. silangdon

    silangdon Guest

    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.
    silangdon, Mar 21, 2005
    #1
    1. Advertising

  2. silangdon

    Ben Pfaff Guest

    silangdon <> writes:

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


    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
    Ben Pfaff, Mar 21, 2005
    #2
    1. Advertising

  3. silangdon

    jacob navia Guest

    silangdon wrote:
    > 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.


    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
    jacob navia, Mar 21, 2005
    #3
  4. silangdon wrote:

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


    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
    David Resnick, Mar 21, 2005
    #4
  5. jacob navia <> writes:
    > silangdon wrote:
    >> 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.

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


    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_Keith) <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.
    Keith Thompson, Mar 21, 2005
    #5
  6. silangdon

    jacob navia Guest

    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
    jacob navia, Mar 21, 2005
    #6
  7. silangdon

    tigervamp Guest

    jacob navia wrote:
    > 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 :)


    Just because it's not easy for you doesn't mean it hasn't been done by
    others.

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


    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.

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


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

    > jacob
    tigervamp, Mar 22, 2005
    #7
  8. silangdon

    silangdon Guest

    On Mon, 21 Mar 2005 12:27:08 -0500, David Resnick
    <> wrote:


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



    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.
    silangdon, Mar 22, 2005
    #8
  9. silangdon

    Richard Bos Guest

    "tigervamp" <> wrote:

    > jacob navia wrote:
    > > 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 :)

    >
    > Just because it's not easy for you doesn't mean it hasn't been done by
    > others.


    *Smothers self*

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

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


    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
    Richard Bos, Mar 22, 2005
    #9
    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. Chris Online

    Converting char* to char

    Chris Online, Feb 17, 2004, in forum: C++
    Replies:
    11
    Views:
    649
    Chris Online
    Feb 19, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  3. Alan
    Replies:
    4
    Views:
    735
    Marcus Kwok
    May 23, 2007
  4. hamishd
    Replies:
    12
    Views:
    4,992
  5. Sean Farrow

    converting a char* to a const char*

    Sean Farrow, Aug 6, 2008, in forum: C++
    Replies:
    24
    Views:
    1,005
    Noah Roberts
    Aug 11, 2008
Loading...

Share This Page