Thread: problem while printing unsigned long long int

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10:03 AM
  4. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM
  5. unsigned long long to string
    By DV64h in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2006, 05:17 AM