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 [-
    Wformat]
    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?
    Hans

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

  2. Hans Vlems

    Eric Sosman Guest

    On 9/7/2012 7:43 AM, Hans Vlems wrote:
    > 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 [-
    > Wformat]
    > 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?


    "%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
    d
    "The speed at which the system fails is usually not important."
     
    Eric Sosman, Sep 7, 2012
    #2
    1. Advertising

  3. Hans Vlems <> writes:

    > 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 [-
    > Wformat]
    > 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?


    %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
    checking.

    --
    Ben.
     
    Ben Bacarisse, Sep 7, 2012
    #3
  4. Hans Vlems

    John Coleman Guest

    On Friday, September 7, 2012 7:43:15 AM UTC-4, Hans Vlems wrote:
    > 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 compilerdidn'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 [- Wformat] t2.c:6:3: warning: too many arguments for format [-Wformat-extra-args] The question is what conversion specification isrequired to read and print long long int variables? Hans PS The compiler errors are translated from Dutch so will very likely differ from the original English messages


    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
    #4
  5. Hans Vlems

    ImpalerCore Guest

    On Sep 7, 7:43 am, Hans Vlems <> wrote:
    > 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 [-
    > Wformat]
    > 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?
    > Hans
    >
    > PS
    > The compiler errors are translated from Dutch so will very likely
    > differ from the original English messages


    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.

    \code
    /*
    * 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
    #endif
    \endcode

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

    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
    #5
  6. Hans Vlems

    Hans Vlems Guest

    On 7 sep, 15:38, ImpalerCore <> wrote:
    > On Sep 7, 7:43 am, Hans Vlems <> wrote:
    >
    >
    >
    >
    >
    > > 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 [-
    > > Wformat]
    > > 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?
    > > Hans

    >
    > > PS
    > > The compiler errors are translated from Dutch so will very likely
    > > differ from the original English messages

    >
    > 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.
    >
    > \code
    > /*
    >  * 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
    > #endif
    > \endcode
    >
    > Then you just use it like the rest of the <inttypes.h> fprintf
    > modifiers.
    >
    > 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.- Tekst uit oorspronkelijk bericht niet weergeven -
    >
    > - Tekst uit oorspronkelijk bericht weergeven -


    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
     
    Hans Vlems, Sep 7, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,237
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,686
    Old Wolf
    Jan 20, 2004
  3. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    40,919
    Joerg Schwerdtfeger
    Apr 2, 2004
  4. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,223
    Peter Shaggy Haywood
    Sep 20, 2005
  5. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,122
    David Thompson
    Jul 28, 2008
Loading...

Share This Page