How to output long long or int64?

B

Betaver

I just want to output a long long or int64 variable use printf
function.But if I use printf("%ld",x); I can't output an x more than
long.


I can only output it like this:


long long a,b,c;
a=c/100000000;
printf("%ld",a);
b=c%100000000;
printf("%08ld\n",b);


How can I output it in an regular way?
 
Z

Zara

I just want to output a long long or int64 variable use printf
function.But if I use printf("%ld",x); I can't output an x more than
long.


I can only output it like this:


long long a,b,c;
a=c/100000000;
printf("%ld",a);
b=c%100000000;
printf("%08ld\n",b);


How can I output it in an regular way?

long->printf("%ld"...
long long->printf("%lld"...

Intuitive.

Best regards,

-- Zara
 
J

Jordan Abel

I just want to output a long long or int64 variable use printf
function.But if I use printf("%ld",x); I can't output an x more than
long.


I can only output it like this:


long long a,b,c;
a=c/100000000;
printf("%ld",a);
b=c%100000000;
printf("%08ld\n",b);


How can I output it in an regular way?


"int64" is non-standard, and probably takes a non-standard format
specifier.

%lld for long long.
 
B

Betaver

Zara:
I wrote:
#include <stdio.h>
long long a;
int main()
{
a=999999999;
a*=5;
pfrintf("%lld",a);
}
But it outputs 705032699, why?
 
B

Betaver

To Jordan Abel:
It's my mistake.I may wrote "__int64", not "int64"

I just get a way to output:
printf("%I64d",c);
It works well.

The reason "%lld" can't work is ä»–that my Dev-C++ didn't support
it.(Somebody says)
 
B

Betaver

Zara:
I use Dev-C++ 4.9.9.2, Gcc 3.4.2

#include <stdio.h>
long long a;
int main()
{
a=999999999;
a*=5;
printf("%lld",a);
}

It outputs 705032699, why?
 
A

Antonio Contreras

Zara:
I wrote:
#include <stdio.h>
long long a;
int main()
{
a=999999999;
a*=5;
pfrintf("%lld",a);
}
But it outputs 705032699, why?

Probably long long is 32 bits wide in your system. Or you declared a as
being long instead of long long.

(99999999999 * 5) % (2^32) = 705032699

HTH
 
K

Keith Thompson

Antonio Contreras said:
Probably long long is 32 bits wide in your system. Or you declared a as
being long instead of long long.

(99999999999 * 5) % (2^32) = 705032699

Not likely. In C99, long long is required to be at least 64 bits.
Some C90 implementations support long long as an extension, but
there's not much point in making it 32 bits, and I've never heard of
an implementation that did so.

Most likely the runtime library's implementation of printf() doesn't
support "%lld", even if the compiler supports long long.
 
R

Richard Bos

I just want to output a long long or int64 variable use printf
function.But if I use printf("%ld",x); I can't output an x more than
long.

If you have C99, "%lld" will output a long long, and "%"PRId64 will
print an int64_t. Other macros in <inttypes.h> will provide similar
specifiers for other sized integers, least and fast types, and so on.

If you are using long long as an extension in a pre-C99 compiler, I'm
afraid this newsgroup can't help you; you'll have to read your manual.

Richard
 
Z

Zara

Zara:
I use Dev-C++ 4.9.9.2, Gcc 3.4.2

#include <stdio.h>
long long a;
int main()
{
a=999999999;
a*=5;
printf("%lld",a);
}

It outputs 705032699, why?

Just tried it. In GCC 3.4.2 the long long type hast 8 bytes, as
required by the standard, but it seems printf is not updated or is
broken when working with long long.

I don´t know if it works in later versions.

Sorry,

-- Zara
 
W

websnarf

Betaver said:
To Jordan Abel:
It's my mistake.I may wrote "__int64", not "int64"

I just get a way to output:
printf("%I64d",c);
It works well.

The reason "%lld" can't work is ä»–that my Dev-C++ didn't support
it.(Somebody says)

The problem is that C99 is a non-adopted standard, and int64_t is
really something that comes from C99, not C90. This lack of a standard
means that older compilers supported 64bits in various different ways.
(Maybe when the next C++ standard comes out, stdint.h and inttypes.h
will becomes widely deployed, however its not guaranteed, and it does
nothing for current C compilers.)

A stop gap solution you can use that will work with your compiler today
is the following:

http://www.pobox.com/~qed/pstdint.h

Then you can do something like:

#include "pstdint.h"
...
int64_t v = INT64_C (9999999999999);
printf ("%" PRINTF_INT64_MODIFIER "d\n", v);

The point being that some compilers use "%lld", and others use "%I64d",
and pstdint.h tries to give you the right one.
 
A

Antonio Contreras

Zara said:
Just tried it. In GCC 3.4.2 the long long type hast 8 bytes, as
required by the standard, but it seems printf is not updated or is
broken when working with long long.

I don´t know if it works in later versions.

Sorry,

Which options did you use. I compiled the above program with gcc 3.3.6
and it worked perfectly well. The command line arguments I provided
were:

gcc -W -Wall -std=c99 -pedantic
 
B

Betaver

Thanks for all the excellent answers.
My complier is gcc 3.4.2 and can't use another one, so I decide output
like this:
printf("%I64d",c);
Is it all right?
 
K

Keith Thompson

Zara said:
Just tried it. In GCC 3.4.2 the long long type hast 8 bytes, as
required by the standard, but it seems printf is not updated or is
broken when working with long long.

I don´t know if it works in later versions.

<SEMI-OT>
printf is part of the runtime library; it's not part of gcc.
</SEMI-OT>
 
Z

Zara

Which options did you use. I compiled the above program with gcc 3.3.6
and it worked perfectly well. The command line arguments I provided
were:

gcc -W -Wall -std=c99 -pedantic

The same parameters. But the porblem is not in the compiler, but in
the library. Do you use the GCC 3.3.6 provided one, or another one?
Maybe printf broke somewhere between 3.3.6 and 3.4.2

-- Zara
 
B

Betaver

Options:
g++.exe "E:\Work\Test.cpp" -o "E:\Work\Test.exe" -g3
-I"D:\Program\DevCpp\include\c++\3.3.1\backward"
-I"D:\Program\DevCpp\include\c++\3.3.1\mingw32"
-I"D:\Program\DevCpp\include\c++\3.3.1"
-I"D:\Program\DevCpp\lib\gcc\mingw32\3.4.2\include"
-I"D:\Program\DevCpp\include\c++\3.4.2\backward"
-I"D:\Program\DevCpp\include\c++\3.4.2\mingw32"
-I"D:\Program\DevCpp\include\c++\3.4.2" -I"D:\Program\DevCpp\include"
-L"D:\Program\DevCpp\lib" -g3

If I am in a tournament the options must be like this.
 
F

Flash Gordon

Zara wrote:

The same parameters. But the porblem is not in the compiler, but in
the library. Do you use the GCC 3.3.6 provided one, or another one?
Maybe printf broke somewhere between 3.3.6 and 3.4.2

There is no such thing as the gcc provided library. gcc uses whatever
library the system has or it is told to use, and this varies depending
on the OS you are using.
 
F

Flash Gordon

Betaver said:
Also what does "-g3" means?

1) Please provide context. There is no guarantee that people have seen
or will ever see the article you are plying to, so your article needs to
be understandable in complete isolation. Search this group for "Google
context" to find how to do this and to see how often we have told people
about this. Then complain at Google for providing such a stupidly broken
interface.

2) If you want information about a specific compiler ask where that
compiler is topical, such as gnu.gcc.help, although I would suggest
reading the manual first, since the information you seek is in there.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top