see i have one complex code but i m stuck up some where so i m just giving you part of that..
i have one function
function(int a,uint64_t b,int c);
when i was calling function
uint64_t b;
int c;
printf("enter b");
scanf("%d",&b); // i m giving 1
printf("enter c");
scanf("%d",&c); // i m giving 2
function(0xcd32ab00 ,b ,c);
in that function definition i m comparing value b with one another parameter like
/* magicNum is type uint64_t type & it has value 1 */
if(magicNum == b)
{
// do something
}
But that “do something” does not happen;
when i m printing magicNum & b both have value 1 so can not understand why this happen.
when i write
if((uint64_t)magicNum == (uint64_t)b)
{
// do something
}
this works perfectly.
i know i m doing one silly mistake but i m not getting…plz help me…
i m working on 32 bit linux system
There was something wrong with your original code that you didn’t show us. That’s because the following works fine:
outputting:
Based on your update that you’re using
scanf("%d")to get the values, this is a fairly well known problem to those of us that have been warped from decades of C usage 🙂When you provide
%das a format specifier, it expects a pointer to aninttype, and it will write to that type. If the pointer you give it is to a type twice as wide (say 64 bits instead of 32), it will only write to half of it.You can see this here:
which outputs
-4294967295when you enter1(because it hasn’t touched the other half of the variable, which is still full of 1-bits).If you change the
scanfformat string to use%llu, it works fine.Keep in mind that this is only because my
unsigned long longvalues are 64 bits wide, just like myunit64_tones. For portability, you should be using the format specifier macros located ininttypes.h. Foruint64_t, the correct one would beSCNu64. The macro names are formed by using:PRIforprintfformat strings orSCNforscanfformat strings.d,i,o,u,xorXto specify signed decimal (dandi), unsigned octal, unsigned decimal, unsigned lower-case hex and unsigned upper-case hex.LEAST,FAST,MAXorPTRfor different variable types.Nwhich is the bit size.SCNu64expands to"llu"on my implementation but that’s not necessarily the case everywhere. On a system that had 64 bitintand 256-bitlong long, it would most likely be"u"(since it equates to anunsigned int).Sample code showing how to use these format strings is shown below:
As to why your
printfmay print out the reduced value, see this code:which outputs:
I suspect you’re printing it with the lower-size format string as well. When you do that, it’s actually an alignment problem. Because your processor is little-endian, it works by accident (the first 32 bits of a 64 bit value are the least significant bits, the
5in the example above).But it will stuff up any arguments after that point in your printf because you’ve pushed 64 bits onto the stack and
printfonly consumes 32, hence the misalignment.See here and here for earlier answers explaining how this works (or, more correctly, doesn’t work).