Newbie data types question in printf vs iostream

M

Mark Bruno

When I declare a 64 bit integer (long long or __int64), it seems to have the
same boundaries as a 32 bit int while using printf, but the correct 64 bit
boundaries when using cout. Here's my code:

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
long long a = 2147483647; //The max value for a signed 32 bit int
long long b = 9223372036854775807; //The max value for a signed 64 bit int
printf("%d %d %d\n", a, a+1, b);
cout << a << " " << a+1 << " " << b << endl;
return 0;
}
 
A

Arthur Gold

Mark said:
When I declare a 64 bit integer (long long or __int64), it seems to have the
same boundaries as a 32 bit int while using printf, but the correct 64 bit
boundaries when using cout. Here's my code:

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
long long a = 2147483647; //The max value for a signed 32 bit int
long long b = 9223372036854775807; //The max value for a signed 64 bit int
printf("%d %d %d\n", a, a+1, b);
cout << a << " " << a+1 << " " << b << endl;
return 0;
}
Because that's what you're *telling* it to do; `%d' is for printing
`int'. The format specifier you want is `%lld' for long long.

[The C++ stream operators get it `right' because of overloading.]

HTH,
--ag
 
S

Steven Green

When I declare a 64 bit integer (long long or __int64), it seems to have the
same boundaries as a 32 bit int while using printf, but the correct 64 bit
boundaries when using cout. Here's my code:

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
long long a = 2147483647; //The max value for a signed 32 bit int
long long b = 9223372036854775807; //The max value for a signed 64 bit int
printf("%d %d %d\n", a, a+1, b);
cout << a << " " << a+1 << " " << b << endl;
return 0;
}

A 64bit noob here.
Is long long standard or is it really just long.

For g++ the compiler will treat long as a 64bit int if compiled with
the -m64 option (although my compiler hasn't been compiled to support it)

I would guess that the printf function doesn't know anything about a
long long and is merely treating it as though it were a 32bit int.
 
A

Arthur Gold

Steven said:
A 64bit noob here.
Is long long standard or is it really just long.

Good catch.

The type `long long' became standard in C99; it is not part of standard
C++ (though it's likely to become the case at some point). In any event,
it's an extension that's frequently available.
For g++ the compiler will treat long as a 64bit int if compiled with
the -m64 option (although my compiler hasn't been compiled to support it)

I would guess that the printf function doesn't know anything about a
long long and is merely treating it as though it were a 32bit int.
See my reply elsethread.

HTH,
--ag
 
M

Mark Bruno

D'OH!!! I forgot to use %llu!!! I'm so stupid! Thanks for pointing out my
error.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top