Converting long long to char

S

silangdon

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

Ben Pfaff

silangdon said:
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'.
 
J

jacob navia

silangdon said:
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
 
D

David Resnick

silangdon said:
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
 
K

Keith Thompson

jacob navia said:
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.
 
J

jacob navia

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
 
T

tigervamp

jacob said:
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).
 
S

silangdon

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

Richard Bos

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

*Smothers self*
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top