By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,930 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,930 IT Pros & Developers. It's quick & easy.

How to output long long or int64?

P: n/a
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?

Nov 15 '05 #1
Share this Question
Share on Google+
26 Replies


P: n/a
On 10 Nov 2005 23:12:17 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:
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
Nov 15 '05 #2

P: n/a
On 2005-11-11, Be*****@gmail.com <Be*****@gmail.com> wrote:
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.
Nov 15 '05 #3

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

Nov 15 '05 #4

P: n/a
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)

Nov 15 '05 #5

P: n/a
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?

Nov 15 '05 #6

P: n/a
Be*****@gmail.com wrote:
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

Nov 15 '05 #7

P: n/a
"Be*****@gmail.com" <Be*****@gmail.com> wrote:
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
Nov 15 '05 #8

P: n/a
"Antonio Contreras" <an*****@gmail.com> writes:
Be*****@gmail.com wrote:
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


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.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #9

P: n/a
On 11 Nov 2005 00:05:23 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:
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
Nov 15 '05 #10

P: n/a
Betaver wrote:
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.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #11

P: n/a
Zara wrote:
On 11 Nov 2005 00:05:23 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:
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,


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

Nov 15 '05 #12

P: n/a
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?

Nov 15 '05 #13

P: n/a
Zara <yo****@terra.es> writes:
On 11 Nov 2005 00:05:23 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:
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.


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

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #14

P: n/a
On 11 Nov 2005 02:07:36 -0800, "Antonio Contreras" <an*****@gmail.com>
wrote:
Zara wrote:
On 11 Nov 2005 00:05:23 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:
>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,


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
Nov 15 '05 #15

P: n/a
But if I'll always use this header file and its printf("%I64d") works
well, is it OK?

Nov 15 '05 #16

P: n/a
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\inclu de"
-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.

Nov 15 '05 #17

P: n/a
Also what does "-g3" means?

Nov 15 '05 #18

P: n/a
Zara wrote:

<snip>
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #19

P: n/a
Betaver wrote:
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.

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #20

P: n/a
"Betaver" <Be*****@gmail.com> wrote:
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?


Not in C, it isn't. Maybe in Ganuck, but that's off-topic here; ask in
gnu.gcc.help (If I've remembered the name correctly; readily findable,
anyway).

Richard
Nov 15 '05 #21

P: n/a
Betaver wrote:
To Jordan Abel:
It's my mistake.I may wrote "__int64", not "int64"


Looks like you are trying quote messages using Google.

I think you will find the information below of value

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #22

P: n/a
Betaver wrote:
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?

Have you tried..

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

...a real C program?

With GCC 3.1 (DJGPP) this prints..
4999999995

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #23

P: n/a
Joe Wright wrote: With GCC 3.1 (DJGPP) this prints..
4999999995

Maybe the problem is my header file. But "%lld" isn't supported here.
Certainly "%I64d" works well.

Nov 15 '05 #24

P: n/a
>Joe Wright wrote:
With GCC 3.1 (DJGPP) this prints..
4999999995


With Dev-C++ 4.9.9.2 Gcc 3.4.2

Nov 15 '05 #25

P: n/a
Betaver wrote:
Joe Wright wrote:
With GCC 3.1 (DJGPP) this prints..
4999999995

With Dev-C++ 4.9.9.2 Gcc 3.4.2

The printf you are calling is Microsoft printf in CRTDLL.DLL
Apparently you are using the mingw version of gcc without glibc.
Use I64 in that system since it works.
Nov 15 '05 #26

P: n/a
Keith Thompson wrote:
Zara <yo****@terra.es> writes:
On 11 Nov 2005 00:05:23 -0800, "Be*****@gmail.com" <Be*****@gmail.com>
wrote:

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.

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

Under windows they are using crtdll.dll by Microsoft.
Nov 15 '05 #27

This discussion thread is closed

Replies have been disabled for this discussion.