64 bit integers and printf

This is a discussion on 64 bit integers and printf within the C Programming forums, part of the General Programming Boards category; This is possibly a very easy question, but most of my experience is in C# and not plain C. I ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    64 bit integers and printf

    This is possibly a very easy question, but most of my experience is in C# and not plain C.

    I have a data file I created using a C# program, with a list of 64-bit integers (type long in C#). I want to read them into a C program. There's some other math that needs to be done to create more 64-bit integers as well.
    However, it seems that either the type long in C is only giving me a 32-bit integer or I'm really messing up my displays. The latter is quite possible. What code would I use in printf to display it?

    As a specific example, I set a variable of type long equal to 2*2^32, then displayed it with printf using %u (is this the right one?). It displayed 2147483648 (which is 2^31). Do I need to use a different data type than long, or is my problem just in my printf?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    uint64_t in <stdint.h> is probably your best bet; failing that then unsigned long long.

    uint64_t foo = 0xdeadbeaf;
    printf( "foo is %llu\n", foo );

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    If your compiler allows specifying 64-bit mode, supply it during the compilation phase. You can find out the size of a long on the target computer with the sizeof(long) command.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can also read this thread, since it's probably the same issue.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    printf( "foo is %llu\n", foo );
    That's for non-Windows systems.

    Windows uses something else (don't remember what).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cyberfish View Post
    That's for non-Windows systems.

    Windows uses something else (don't remember what).
    "%I64u" would be the Windows answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Quote Originally Posted by cyberfish View Post
    That's for non-Windows systems.

    Windows uses something else (don't remember what).
    Oh, this is a Linux machine it's running on.
    Thanks for the answers everyone! I'll give it a try.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    It looks like it's working! The long type on that machine was just 4 bytes, so the unsigned long long gave me what I needed, along with %llu for printf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-28-2009, 02:28 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 09:58 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM

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