long long int and sscanf, printf

Discussion in 'C Programming' started by Hans Vlems, Sep 7, 2012.

  1. Hans Vlems

    Hans Vlems Guest

    I'm moving from the djgpp environment to mingw. This also implies a
    more modern C compiler,
    djgpp supports gcc 4.4.4 and mingw uses gcc 4.7.0 (possibly not the
    most current version but
    that's what I have available right now).
    The change resulted in one error and one I can't figure out.
    A call to sscanf like sscanf(charptr,"%lld",&longlonginteger);
    presented to problem for 4.4.4, neither syntactically nor in resulting
    functionality. The compiler didn't complain and the code behaved as I
    expected so no complaints.

    However gcc 4.7.0 flags an error:

    t2.c:6:3: warning: unknown conversion type character l in format [-
    t2.c:6:3: warning: too many arguments for format [-Wformat-extra-args]

    The question is what conversion specification is required to read and
    print long long int variables?

    The compiler errors are translated from Dutch so will very likely
    differ from the original English messages
    Hans Vlems, Sep 7, 2012
    1. Advertisements

  2. Hans Vlems

    Eric Sosman Guest

    "%lld" is correct, for both printf() and scanf(). However,
    `long long int' was introduced by the C99 standard, and wasn't
    part of the original ANSI/C89/C90 version. Have you specified
    which version of C gcc should compile? Try "-std=c99" on the
    gcc command line, and see if that helps.
    Eric Sosman, Sep 7, 2012
    1. Advertisements

  3. %lld is correct. The only way I can get close to reproducing this error
    is to compiler with -std=c90 -pedantic but then I get a very clear
    message the C90 does not support 'll'. This is gcc 4.6.3, though.

    On a similar issue (though probably not connected to this particular
    problem) I was under the impression that mingw used a native Windows C
    library and because, at the time, MS did not support C99, printing long
    long ints did not work property. That may all be different now, for one
    thing I think MS C compilers understand long long now, but it's worth
    Ben Bacarisse, Sep 7, 2012
  4. Hans Vlems

    John Coleman Guest

    gcc in mingw uses Microsoft's implementation of C's standard library, whichdoesn't support C99 extensions. Thus gcc in mingw doesn't implement changes that C99 made to the standard library. You'll have to find a work-around (e.g. split a long-long into two longs and print them successively)
    John Coleman, Sep 7, 2012
  5. Hans Vlems

    ImpalerCore Guest

    MinGW uses the C Runtime, which has limited C99 support. I have my
    own <stdint.h> wrapper that takes Microsoft's quirks into
    consideration. There's a few ways to work around it, but I found that
    adding a custom fprintf modifier was the cleanest.

    * The Microsoft Runtime library is notorious for using its own
    * custom fprintf modifier prefix for 64-bit integers.
    #if defined(__STDC__) && defined(__STDC_VERSION__)
    # if (__STDC__ && __STDC_VERSION__ >= 199901L)
    # define inttypes_pridlonglong_defined
    # if defined(_WIN32) || defined(__WIN32__)
    # define PRIdLL "I64d"
    # define PRIiLL "I64i"
    # define PRIoLL "I64o"
    # define PRIuLL "I64u"
    # define PRIxLL "I64x"
    # define PRIXLL "I64X"
    # else
    # define PRIdLL "lld"
    # define PRIiLL "lli"
    # define PRIoLL "llo"
    # define PRIuLL "llu"
    # define PRIxLL "llx"
    # define PRIXLL "llX"
    # endif
    # endif

    Then you just use it like the rest of the <inttypes.h> fprintf

    printf( "longlongint = %" PRIdLL "\n", longlongint );

    The corresponding SCN versions are left as an exercise. If using
    reserved names worries you, add a prefix.

    Best regards,
    John D.
    ImpalerCore, Sep 7, 2012
  6. Hans Vlems

    Hans Vlems Guest

    Thank you all for your comments,
    I use King's book (C Programming) as reference
    and noted that the ll length specifier is C99 specific. The fact that
    a newer
    version of gcc flagged an error was somewhat of a surprise. The
    explanation is
    clear and quite obvious (a hindsight attribute :).
    Thanks for the workaround John!
    Hans Vlems, Sep 7, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.