printf conversion characters for long longs.

This is a discussion on printf conversion characters for long longs. within the C Programming forums, part of the General Programming Boards category; What is the printf conversion character for a long long? And the one for a long double? I just noticed ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    printf conversion characters for long longs.

    What is the printf conversion character for a long long? And the one for a long double? I just noticed that %d and %x.f (x number of decimal places) don't work with long longs and long doubles.

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    ll (two lowercase L) for a long long int, L for a long double (in standard C99 at any rate).

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    I don't understand. I am trying to output a long double using printf. I tried %L, %.0L, %Lf, %.0Lf, %L.0f, %.0fL, and none of them work!?!?!
    Last edited by samus250; 04-13-2008 at 06:54 PM. Reason: typos

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using minGW on windows? See here;
    http://coding.derkeiler.com/Archive/.../msg04086.html

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Yup, that's me, MinGW on Windows. But I still didn't get the conversion character I need for the printf function from that post.

    [edit] Or is it simply not possible using MinGW?
    Last edited by samus250; 04-13-2008 at 07:14 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by samus250 View Post
    Yup, that's me, MinGW on Windows. But I still didn't get the conversion character I need for the printf function from that post.
    That's because there isn't one. The MS runtime doesn't support the 10-byte long double that gcc uses. To print it, cast it to a double and hope that it's within range.
    http://www.mingw.org/MinGWiki/index.php/long%20double

    Or I suppose you could write your own printf-like function just for long doubles....

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by tabstop View Post
    That's because there isn't one. The MS runtime doesn't support the 10-byte long double that gcc uses. To print it, cast it to a double and hope that it's within range.
    http://www.mingw.org/MinGWiki/index.php/long%20double

    Or I suppose you could write your own printf-like function just for long doubles....
    Crap.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just for you (and for me, since I have MinGW on Windows, too), this appears to work -- although I hacked it together in the last ten minutes so you never know:
    Code:
    char *printf_ld(long double arg, char answer[15]) {
    
        /* For my own sake: scientific notation with six digits */
        double mantissa, exponent, lg;
        lg = log10l(fabs(arg));
        exponent = floor(lg);
        mantissa = pow(10, lg-exponent);
        mantissa = copysign(mantissa, arg);
        sprintf(answer, "%.6fe%+04g", mantissa, exponent);
        return answer;
    }
    Use at your own peril.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Cool, thanks a lot! I'll give a look at that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  3. 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
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM

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