473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf a 64-bit number

GCC 3.3, XCode:
#include <iostream>
#include <stdint.h>

int main (int argc, char * const argv[])
{
uint64_t a=10123123123LL;
printf("%d\n", a);
std::cout << a;
std::cout << "\n";
return 0;
}

The printf statement gives me 2 (which is the upper 32 bits of a). The
cout gives me the correct result. Is this expected behaviour? Surely
64 bit integers are de rigeur now.

I'm generally trying to use C and not C++ for portability and speed.
Jul 23 '05 #1
3 8326
Richard Cavell wrote:
GCC 3.3, XCode:
#include <iostream>
#include <stdint.h>

int main (int argc, char * const argv[])
{
uint64_t a=10123123123LL;
printf("%d\n", a);


printf("%lld\n", a);

Best
Darek
Jul 23 '05 #2
Richard Cavell wrote:
GCC 3.3, XCode:
#include <iostream>
#include <stdint.h>

int main (int argc, char * const argv[])
{
uint64_t a=10123123123LL;
printf("%d\n", a);
std::cout << a;
std::cout << "\n";
return 0;
}

The printf statement gives me 2 (which is the upper 32 bits of a). The
cout gives me the correct result. Is this expected behaviour?
Yes. You specified %d to printf, which means that you promised to pass an
int. But since you didn't actually pass an int, the result will be wrong.
Surely 64 bit integers are de rigeur now.

I'm generally trying to use C and not C++ for portability and speed.


What makes you believe printf is more portable and faster than cout? Do you
have any evidence that proves this?

Jul 23 '05 #3

Richard Cavell wrote:
GCC 3.3, XCode:
#include <iostream>
#include <stdint.h>

int main (int argc, char * const argv[])
{
uint64_t a=10123123123LL;
printf("%d\n", a);
std::cout << a;
std::cout << "\n";
return 0;
}

The printf statement gives me 2 (which is the upper 32 bits of a). The cout gives me the correct result. Is this expected behaviour? Surely 64 bit integers are de rigeur now.

I'm generally trying to use C and not C++ for portability and speed.


Well, uint64_t isn't really portable. But I guess this shows:
with C it's easy to be wrong everywhere. You're probably very fast
in producing the wrong answer, but C++ is even faster:
in main() { } also produces the wrong output. It's also smaller.
The moral: being right is more important than petty concerns.

HTH,
Michiel Salters

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7960
by: Gernot Frisch | last post by:
Hi, I want to represent a double in char* as accurate as possible, but as short as possible: double char* 10000 1E5 1000.00123 1000.00123 ....
16
19684
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
6
3545
by: pasukaru | last post by:
I need to work with big integers ( around 2.^64), and I know it is not possible to have integer that big. So I use "double" instead, and it is ok with calculations. But when I want to print them...
16
2756
by: Gernot Frisch | last post by:
Hi, class MyString { char* m_data; public: MyString(const char* c) { m_data = new char; strcpy(m_data, c);
34
15837
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
18
422
by: Shane | last post by:
Hi all, I am learning C, and I have a problem with printf. I have a pointer that points to binary data, and I want to print that data out. If I cast the pointer to (char *) then the data is...
2
3190
by: wombat | last post by:
Is there a way to prevent rounding when using printf for print a double/float? Example: double x=3.51; printf("%.0f\n", x); The output of this is 4, when I want it to be 3.
7
18950
by: PawelCarqowski | last post by:
Hallo group members, Do You know any conversion specification for this. I imagine: struct timeval time; printf("%T\n", time); regards, Pawel
5
2755
by: Jerry Newsome | last post by:
If sizeof(int) = sizeof(long), is this undefined behavior or is it okay? #include <stdio.h> int main(void) { int i = 5; long l = 7; printf("i = %ld\n", i); printf("l = %d\n", l); return 0;
14
3950
by: Sheldon | last post by:
Hi, Can anyone tell me why this script file.c and file.h causes a core dump when it is compiled and run? Any help is appreciated. Sheldon snip....
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.