if statement problem
see i have one c开发者_如何学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:
#include <stdio.h>
static void fn (int a, uint64_t b, int c) {
uint64_t magicNum = 1;
if (magicNum == b)
puts ("They match!");
if ((uint64_t)magicNum == (uint64_t)b)
puts ("They still match!");
}
int main (void) {
fn (0xcd32ab00 ,1 ,2);
return 0;
}
outputting:
They match!
They still match!
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 %d
as a format specifier, it expects a pointer to an int
type, 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:
int main (void) {
uint64_t x = 0xffffffffffffffffULL;
printf ("Enter your number: ");
scanf ("%d", &x); // signed int
printf ("%llu\n", x); // unsigned long long
return 0;
}
which outputs -4294967295
when you enter 1
(because it hasn't touched the other half of the variable, which is still full of 1-bits).
If you change the scanf
format string to use %llu
, it works fine.
Keep in mind that this is only because my unsigned long long
values are 64 bits wide, just like my unit64_t
ones. For portability, you should be using the format specifier macros located in inttypes.h
. For uint64_t
, the correct one would be SCNu64
. The macro names are formed by using:
PRI
forprintf
format strings orSCN
forscanf
format strings.d
,i
,o
,u
,x
orX
to specify signed decimal (d
andi
), unsigned octal, unsigned decimal, unsigned lower-case hex and unsigned upper-case hex.- optional
LEAST
,FAST
,MAX
orPTR
for different variable types. N
which is the bit size.
SCNu64
expands to "llu"
on my implementation but that's not necessarily the case everywhere. On a system that had 64 bit int
and 256-bit long long
, it would most likely be "u"
(since it equates to an unsigned int
).
Sample code showing how to use these format strings is shown below:
#include <stdio.h>
#include <inttypes.h>
int main (void) {
uint64_t x = 0xffffffffffffffffULL;
printf ("Enter your number: ");
scanf ("%" SCNu64, &x);
printf ("%" PRIu64 "\n", x);
return 0;
}
As to why your printf
may print out the reduced value, see this code:
#include <stdio.h>
#include <inttypes.h>
int main (void) {
uint64_t x = 0xffffffffffffffffULL;
printf ("Enter your number: ");
scanf ("%u", &x);
printf ("uint64_t = %" PRIu64 "\n", x);
printf ("uint64_t = %" PRIu64 " (when and'ed)\n", x & 0xffffffffU);
printf ("uint = %u\n", x);
return 0;
}
which outputs:
Enter your number: 5
uint64_t = 18446744069414584325
uint64_t = 5 (when and'ed)
uint = 5
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 5
in 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 printf
only consumes 32, hence the misalignment.
See here and here for earlier answers explaining how this works (or, more correctly, doesn't work).
arguments a and b swapped ??
function(0xcd32ab00 ,1 ,2);
should be
function(1, 0xcd32ab00 ,2);
精彩评论