Scanf and long long

This is a discussion on Scanf and long long within the C Programming forums, part of the General Programming Boards category; From what I've just tested now and from scouring the net it appears that scanf is broken when it comes ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Scanf and long long

    From what I've just tested now and from scouring the net it appears that scanf is broken when it comes to "long long" types. Scanning in a long long using "%lld" and "%ld" crashed for me. I'm using MingW on windows. Does anyone know if this is fixed in the latest version of GCC?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    MinGW uses the microsoft run-time, so you need to use the microsoft specific conversion, not the ANSI conversion.

    It's something like "%I64d", but check the relevant MSDN page.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Yep, you're right, using "%I64d" works now. This raises two further questions though:
    Does Microsoft officially support a "long long" type? I.E. should "__int64" be used instead of "long long" when programming for windows (since "long long" by ISO standards is not garaunteed to be just 64bits)?
    And if so, how does Microsoft then handle an "unsigned long long" type for scanf/printf? "%uI64" or "%uI64d" etc do not work.. here is some code that tests some of these issues:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
    	long long int test;
            printf("Enter long long int:\n");
    	scanf("%I64d", &test);
    	printf("%I64d", test);
    	
    	printf("\n%I64d", LLONG_MAX);
    	printf("\n%ull", ULLONG_MAX);
    	printf("\n%uI64", ULLONG_MAX);
    }
    The last two produce interesting results...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > "%uI64" or "%uI64d" etc do not work
    I64 is a prefix, like ll, so it would need to be "%I64u"

    > Does Microsoft officially support a "long long" type? I.E. should "__int64" be used instead of "long long" when programming for windows
    > (since "long long" by ISO standards is not garaunteed to be just 64bits)?
    Well the relationship between long long and __int64 is something only the maintainers of MinGW have to worry about. For the most part, long long will do as you expect.

    It's basically a "bug" in MinGW in relying on a non-C99 compliant library to implement C99 features.

    Though if you're calling some windows specific API which uses __int64, then you should probably use it.

    Interestingly, your man page URL indicates that "ll" is a valid prefix. I guess that at some point that the MinGW maintainers will decide to link with a later version of msvcrd.dll.
    You might be able to do this manually, by editing some option. I don't know, I've never tried it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf doesn't like order of input
    By yougene in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 12:59 PM
  2. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 01:48 AM
  3. Is this the perfect input function?
    By Brain Cell in forum C Programming
    Replies: 30
    Last Post: 10-20-2004, 08:41 PM
  4. Is long long int a valid Data Type
    By shiv_tech_quest in forum C Programming
    Replies: 2
    Last Post: 11-12-2003, 08:59 AM
  5. malloc crashing program
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-08-2003, 02:52 PM

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