473,657 Members | 2,458 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 8348
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
7979
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
19697
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 to be used for whatever reason, and memory allocation cannot be used - mainly it's become for me an exercise in interpreting printf-like format strings, and i thought i'd post it here to see if anyone can point out any bugs in it - i.e. anything...
6
3554
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 on screen or in a file, there is a problem... For example, if I write this : printf("result: %lf\n",1234567890987654321.012345678);
16
2770
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
15905
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 printed out in the UniCode(?) equivalent of the data. If I try to cast to (int *) then I only get the address of the pointer (I think). What I really want is all the ones and zeros to be printed out. Any help appreciated TIA --
2
3198
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
18986
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
2764
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
3970
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
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2743
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 we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.