problem while printing unsigned long long int

This is a discussion on problem while printing unsigned long long int within the C Programming forums, part of the General Programming Boards category; Hello, When I try to print an unsigned long long int I get a different number displayed. This is the ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    problem while printing unsigned long long int

    Hello,

    When I try to print an unsigned long long int I get a different number displayed.
    This is the code I'm using:
    Code:
    int main(int argc, char *argv[]) {
    
        printf ( "ULLONG_MAX: %llu\n", ULLONG_MAX );
        unsigned long long int power=atoi(argv[1]);
        unsigned long long int shift_power=0;
    
        printf("normal, %llu\n", power);
        shift_power=power<<10;
        printf("after shift, %llu\n", shift_power);
    ...
    running it with:
    > ./test 35184372088832

    the result is:
    ULLONG_MAX: 18446744073709551615
    normal, 0
    after shift, 0
    Any idea please? do you know why I'm getting 0 for both?
    thx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    What is your compiler and OS? On Windows, you may need a different format specifier, i.e., %I64u instead of the standard %llu. Also, you should be using strtoull for the conversion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hi

    thx for reply. You're right atoi converts to integer !!
    My OS is Redhat 6 and I use gcc. I changed my code by using strtoull instead of atoi and now is better but not perfectly fine:
    Code:
    unsigned long long int power=strtoull(argv[1], NULL, 16);
    In this case I get a different number like:
    > ./test 35184372088832
    got back
    normal, 1913161778
    after shift, 1959077660672

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hello

    solved!
    actually I forgot to include the stdlib.
    Moreover the int base is 10 and not 16 as I wrote

    thx for the tip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  2. C++ - unsigned long long int comparison
    By grimuth in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2010, 10:20 AM
  3. unsigned long long division print
    By Kempelen in forum C Programming
    Replies: 4
    Last Post: 01-30-2009, 09:03 AM
  4. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 03:02 AM
  5. unsigned long long to string
    By DV64h in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2006, 05:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21