开发者

What format specifier to use for printing "unsigned long long" in C with getting truncated values on the console?

typedef unsigned long long IMSI;

IMSI imsi;

when i am trying to print this using %llu as a format specifier, i am getting a rather unrelated value.

What can i do to remove this problem?

I am also using gcc 4.3.3 I though there might be a problem with the tracing mechanism that i have been using, but i am getting the same problem even when using printf.

    imsiAsInt = 9379666465 ;
    brrm_trace(ubm_TRACE_BRRM_UECTRL,ubm_TRACE_INFO,
              UEC_IUH_ACCACHE_ENTRY_FOUND,imsiAsInt, status.ueRegCause,
    开发者_运维问答          mCacheEntries.size());
    printf("printf:UEC_IUH_ACCACHE_ENTRY_FOUND=%llu, sizeof(IMSI)=%d\n",
            imsiAsInt,sizeof(IMSI));

This gives following output UEC_IUH_ACCACHE_ENTRY_FOUND Imsi=789731873,UeRegCause=1,CurSize=5 -->The trace printf:UEC_IUH_ACCACHE_ENTRY_FOUND=789731873, sizeof(IMSI)=8 ---> when using printf

Also for smaller values in 7 digits i am not getting any issue.


Which compiler are you using? The following program

#include <stdio.h>
int main()
{
  unsigned long long x;

  x = 12345;
  printf("Value: %llu\n", x);

  x = -1;
  printf("Value: %llu\n", x);

  return 0;
}

does give the expected output:

Value: 12345
Value: 18446744073709551615

on Linux with gcc 4.4.3


This could be a problem:

imsiAsInt = 9379666465 ;

[Warning] integer constant is too large for 'long' type 

Try 9379666465ll


You didn't say what OS or compiler you're using, and you haven't posted the code, so giving a correct answer is not easy. I'll have a stab at it though and guess that you're using an old version of MSVC that doesn't support the standard printf format specifiers for long long and you may therefore have to use the non-standard Microsoft alternative of %Lu to get the desired result.

For future reference you should post your code, and give enough detail for people to answer e.g. what OS and compiler you are using. As others have noted you should also do something about your accept rate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜