Displaying a 'long long' with printf

This is a discussion on Displaying a 'long long' with printf within the C Programming forums, part of the General Programming Boards category; Hi all - I am having some difficulty displaying a variable of type 'long long' with printf(). My code is ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Phoenix, AZ
    Posts
    5

    Displaying a 'long long' with printf

    Hi all - I am having some difficulty displaying a variable of type 'long long' with printf(). My code is as follows:

    Code:
    /* File: largenum.c */
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            // note that the last two characters are lowercase 'L', not '1'
            long long num = 317584931803ll;
    	
    	printf("Very Large Number: %lld \n", num );
    	printf("Sizeof (long long) = %d\n", sizeof(long long) );
    	return 0;
    }
    This compiles with no warnings for me (mingw gcc 3.4.2). However, the output I recieve is always this:
    Very Large Number: -242648101
    Sizeof (long long) = 8
    If I convert the project to c++ and use cout to display num, it shows exactly the number I want it to, but I already am quite familiar with C++, and I want to learn how to do this in C.
    The size of a long long is 8 bytes (as it should be), so I am fairly certain that the problem lies in my usage of printf(). If I convert it to an 'unsigned long long', and printf("%llu ", num) -- , I at least get a POSITIVE number, but it is still not the correct number. Any help would be appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I prefer to use the definitions from stdint.h:
    Code:
    itsme@dreams:~/C$ cat bigone2.c
    #include <stdio.h>
    #include <stdint.h>
    
    int main(void)
    {
      uint64_t num = UINT64_MAX;
      int64_t num2 = 317584931803ll;
    
      printf("%llu\n", num);
      printf("%lld\n", num2);
    
      return 0;
    }
    Code:
    itsme@dreams:~/C$ ./bigone2
    18446744073709551615
    317584931803
    Last edited by itsme86; 12-27-2004 at 12:16 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Phoenix, AZ
    Posts
    5
    That did the trick! Thanks alot. I guess, from reading this link, that I needed to declare it as 'long long int '.
    Last edited by trinitrotoluene; 12-27-2004 at 12:41 PM.

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by trinitrotoluene
    Hi all - I am having some difficulty displaying a variable of type 'long long' with printf().
    Code:
    /* File: largenum.c */
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            // note that the last two characters are lowercase 'L', not '1'
            long long num = 317584931803ll;
    	
    	printf("Very Large Number: %lld \n", num );
    	printf("Sizeof (long long) = %d\n", sizeof(long long) );
    	return 0;
    }
    This compiles with no warnings for me (mingw gcc 3.4.2).
    Be sure that you are compiling in C99. Add -std=c99 on the command line.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    [EDIT] nm [/EDIT]

    gg

  6. #6
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    ok ok ok ok

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Emmanuel Delaha
    Be sure that you are compiling in C99. Add -std=c99 on the command line.
    Is there a different approach to using this technique for really long numbers without using C99?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    C99 is a standard that your compiler will either support, perhaps partially, or not at all. Otherwise the support for a 64bit integer will be a compiler specific feature. For example, MSVC supports 64bit integers using the (compiler-specific) built-in type "__int64".

    ...

    I decided to post my original response - although it's not the root cause of trinitrotoluene's problems, it is a problem that's popped up here a few times in the past. In short, you can use the MS C-runtime library (MSVCRT.DLL) with MinGW - and the version of printf() in this library uses "%I64" instead of "%ll" when specifying a 64bit integer.
    http://www.mingw.org/MinGWiki/index....8634fcd27af45d

    gg

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    Phoenix, AZ
    Posts
    5
    and the version of printf() in this library uses "%I64" instead of "%ll" when specifying a 64bit integer.
    You're absolutely correct! Sorry, itsme86, I jumped the gun when I thought that your solution fixed the problem. It didn't . I was still getting weird output like
    4294967295
    -242648101
    but changing the printf statements to "%I64u" and "%I64d" respectively (as suggested by Codeplug) produced the correct output. Since itsme86 was obviously using a *nix system (or maybe cygwin/msys, but I doubt it), and his code showed the output fine, does that mean that my code with the "%I64u" is now win32 platform specific? If I specify that I want ANSI compliance with the flag -pedantic-errors , I then get two warnings of
    warning: ISO C does not support the `I64' printf length modifier
    Arg. So, is there an ANSI compliant way to display 64 bit integers with printf() ?
    Last edited by trinitrotoluene; 12-27-2004 at 09:15 PM.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    >> does that mean that my code with the "%I64u" is now win32 platform specific?
    Yes.

    >> is there an ANSI compliant way to display 64 bit integers with printf()?
    When using the MS C-runtime (MSVCRT.DLL) - no.

    The only alternative C-runtime for MinGW that I know of is within the Cygwin enviroment (using "newlib").

    gg

  11. #11
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Kleid-0
    Is there a different approach to using this technique for really long numbers without using C99?
    C90 only supports long and unsigned long.

    If you are using an extension, the details belong to the compiler's documentation.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  3. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  4. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 12:32 PM
  5. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM

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