Menu

long double in printf - not implemented ?

2005-03-30
2012-09-26
  • Nobody/Anonymous

    I tried to use long double variables - no success.
    printf prints garbage. Are %Lg, %Lf and such not implemented ?
    Bloodshed C++ ver 4.9.9.2

     
    • Nobody/Anonymous

       
    • Anonymous

      Anonymous - 2005-03-31

      The printf() implementation is Microsoft's. (From MSVCRT.DLL). The format specifiers ars documented here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/crt_format_specification_fields.2d_.printf_and_wprintf_functions.asp.

      I understand that C++ ostream objects (such as cin) handle long double correctly. The C++ library is GNU code, not Microsoft.

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2005-03-31

      This needs to be referenced in the FAQ somewhere. It doesn't come up real frequently, but it does come up once a month or so...

      Wayne

       
    • M.B.

      M.B. - 2005-03-31

      Wayne:
      "This needs to be referenced in the FAQ somewhere."

      I have added it to my new help file's FAQ. I really hope it will be distributed with the next version of Dev-C++, as it contains at least everything that was in the old (current) help file. (Except for the buggy C tutorial.)
      Here's the wording I used:

      Why do long doubles, long long's and unsigned long long's not work?
      This happens if you use C functions such as scanf and printf for input and output. The problem is that MinGW is written on top of Microsoft's MSVCRT.DLL which doesn't have support for these data types.
      Solutions:

      • Use cin and cout from C++ for input and output.
      • Use %I64d for long long and %l64u for unsigned long long. This is non-standard but supported by MSVCRT.
      • Use another compiler.

      Mathias

       
      • Myrddin Bach

        Myrddin Bach - 2008-10-09

        I am using version 4.9.9.2 and am trying to print long long data types in a C program - NOT C++ so I am NOT using cin or cout.

        Is there a way to use printf in a C program to print out a long long data type?

        Ive searched through the threads and tried a LOT of suggested things and nothing suggested seems to work.

        Ive tried I64u, I64d, Llu, llu, lld, Lld, Lf, lf, llx, q, Ld, and probably a few others.

        Code below - anyone have an answer of what I can do?

        include <stdio.h>

        int main(int argc, char *argv[])
        {
        long long x;
        x=40000000000ll;
        printf("%I64u", x);
        return 0;
        }

         
        • cpns

          cpns - 2008-10-10

          I just noticed your code:

          > printf("%I64u", x);

          but x is long long not unsigned long long.

          If you want unsigned long long outoput the code above needs modification. I suggest that you create a separate function :

          char ulltostr( char buffer, unsigned long long ) ;

          Clifford

           
        • cpns

          cpns - 2008-10-10

          This thread is over two years old, but the answer is the same! Have you read the information already in this thread!?

          The C library does not support long long.

          You don't have to learn or use much of C++ to take advantage of its library support for long long, and since the C++ compiler profroma better error checking, it would be a good thing in any case even if the rest of your code used only the C subset.

          However, since there may be some compelling reason to use C only compilation, the rather obvious solution is to write your own long long to string concersion function. For example:

          include <math.h>

          // NOTE: buffer must have at least 21 characters to avoid overrun!!!
          char lltostr( char buffer, long long val )
          {
          int digit ;
          if( val == 0 )
          {
          buffer[0] = '0' ;
          digit = 0 ;
          }
          else
          {
          if( val < 0 )
          {
          buffer[0] = '-' ;
          val = -val ;
          digit = log10( (double)val) + 1 ;
          }
          else
          {
          digit = log10( (double)val) ;
          }

          }
          
          buffer[digit + 1] = '\0' ;
          
          while( val != 0 )
          {
              buffer[digit] = '0' + val % 10 ;
              val /= 10 ;
              digit-- ;
          }
          
          return buffer ;
          

          }

          include <stdio.h>

          int main()
          {
          long long a = 0LL ;
          long long b = -1234567890123456789LL ;
          long long c = 1234567890123456789LL ;
          char longbuf[21] ;
          printf( "%s\n", lltostr( longbuf, a ) ) ;
          printf( "%s\n", lltostr( longbuf, b ) ) ;
          printf( "%s\n", lltostr( longbuf, c ) ) ;
          }

          Note that you could write the above without the log10() call, but using it avoids having to reverse the string by determining the number of digits at the beginning rather than at the end.

          An unsigned long long version will obviously not require the (val < 0) check.

          Clifford

           
        • cpns

          cpns - 2008-10-10

          Sorry again, to avod warnings in the sugegsted code, change teh log10() calls thus:

          digit = (int)log10( (double)val) + 1 ;

          digit = (int)log10( (double)val) ;

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.