tags:

views:

18137

answers:

16
#include <stdio.h>

int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
return 0;
}

Output:

My number is 8 bytes wide and its value is 285212672l. A normal number is 0.

I assume this unexpected result is from printing the unsigned long long int. How do you printf an unsigned long long int?

+20  A: 

Use the ll (el-el) long-long modifier with the u (unsigned) conversion.

printf("%llu", 285212672);
John Downey
Doesn't work on Windows - this is for Linux/UNIX only.
Paul Hargreaves
Or to be precise it's for GNU libc, and doesn't work with Microsoft's C runtime.
Mark Baker
This isn't a Linux/UNIX thing, the "ll" length modifier was added to Standard C in C99, if it doesn't work in "Microsoft C" then it is because they are not standards compliant.
Robert Gamble
Works in Turbo C++ on Windows
Patrick McDonald
See? Windows rules! </sarcasm>
Artelius
I am using Visual C++ 2005 and it works fine
JProgrammer
Works for me in VS2008. Moreover, as far as I remember the MS C Compiler (when set up to compile straight C) is supposed to be C90 compliant by design; C99 introduced some things that not everyone liked.
matthews
+2  A: 

Non-standard things are always strange :)

for the long long portion under GNU it's L, ll or q

and under windows I believe it's ll only

sparkes
A: 

%llu does not work either. Note that normalInt outputs as 0, instead of 5 as it should.

#include <stdio.h>

int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf("My number is %d bytes wide and its value is %llu. A normal number is %d.\n", sizeof(num), num, normalInt);
return 0;
}

Output:

My number is 8 bytes wide and its value is 285212672. A normal number is 0.
superjoe30
A: 

%llu does not work either. Note that normalInt outputs as 0, instead of 5 as it should

@superjoe30: This looks like the %llu is working and printing your unsigned long long int just fine.

it's the %d => normal int which isn't working, which (although I thought %d handled ints just fine, this may change with different compilers etc) you might just need to change to %i

Check the manual for printf for your compiler

Orion Edwards
A: 
vzczc
A: 

@Orion Edwards:

%d is the same as %i, but just to make sure I tried compiling it, and yes, it gives the same results. It is, in fact, the int64 causing problems.

@vzczc:

But I don't want to compile it with 64-bit. It's a 32-bit application.

FYI The code that I have posted here was the output of using gcc.

superjoe30
A: 

std::cout might work for this. (I'm at work and cant test it)

Ed
That's only in C++.
Lehane
A: 

I just compiled your code ( with %llu ) with gcc and the output was the correct one.

Are you passing any options to the compiler?

Juan
+5  A: 

You may want to try using the inttypes.h library that gives you types such as int32_t, int64_t, uint64_t etc. You can then use its macros such as:

uint64_t x;
uint32_t y;

printf("x: %"PRId64", y: %"PRId32"\n", x, y);

This is "guaranteed" to not give you the same trouble as long, unsigned long long etc, since you don't have to guess how many bits are in each data type.

Nathan Fellman
+1  A: 

@superjoe30: What platform are you on? On ubuntu gutsy, your program (with %llu) gives the following output:

$ gcc -o num num.c
$ ./num 
My number is 8 bytes wide and its value is 285212672. A normal number is 5.

I also get the same output on debian etch. Both machines are 32 bit intels.

Jason Day
+1  A: 

That is because %llu doesn't work properly under Windows and %d can't handle 64 bit integers. I suggest using PRIu64 instead and you'll find it's portable to Linux as well.

Try this instead:

#include <stdio.h>
#include <inttypes.h>

int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %"PRIu64". A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}

Output

My number is 8 bytes wide and its value is 285212672. A normal number is 5.
Paul Hargreaves
+1  A: 

In Linux it is %llu and in Windows it is %I64u

Although I have found it doesn't work in Windows 2000, there seems to be a bug there!

Adam Pierce
with windows, (or at least, with the microsoft C compiler for windows) there's also %I64d, %I32u, and %I32d
JustJeff
What does it have to do with Windows 2000? The C library is the one that handles printf.
iconiK
Just what I observed. I wrote an app which used this construct and it worked perfectly on WinXP but spat garbage on Win2k. Maybe it's something to do with a system call that the C library is making to the kernel, maybe it's something to do with Unicode, who knows. I remember having to work around it using _i64tot() or something like that.
Adam Pierce
A: 

on HPUX machine, I have some other problem.

#include<stdio.h>
#include <inttypes.h>
void main()
{
        /*unsigned long var;*/
        unsigned long long var;
        var = 4294967295;
        printf("Sizeof uint = %d\n",sizeof(var));
        printf("var = %"PRIu64"\n",var);
}

output:

#cc one.c
# ./a.out
Sizeof uint = 8
var = 18446744073709551615

how is this possible, i have assigned a value to "var" why is it not getting printed?? Please help me.

+4  A: 

For long long (or __int64) using MSVS, you should use %I64d:

__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b);    //I is capital i
A: 

correct your line to: printf("var = %PRIu64\n",var);

v0ld
A: 

Note that samsung bada's newlib seems not to support "%lld" :

http://developer.bada.com/forum/topic.php?id=2966&amp;replies=7#post-22945

rzr