• AVR Freaks

Hot!Format Specfiers for long double and float

Author
fsonnichsen
Super Member
  • Total Posts : 330
  • Reward points : 0
  • Joined: 2009/08/13 11:00:11
  • Location: 0
  • Status: offline
2021/01/08 19:27:09 (permalink)
0

Format Specfiers for long double and float

XC16 Version 1.6.0
 
Can someone tell me the appropriate format specifiers for sprintf with:
   float ADC;
   long double ADC;
 
Using type %f for both I get different results and both always result in a error messages:
   main.c:93:5: warning: format '%g' expects type 'double', but argument 3 has type 'long double'
   main.c:105:5: warning: format '%f' expects type 'double', but argument 4 has type 'float'
 
I could find nothing in the X16 manual. I tried a few other things. Looking on this forum there are some allusions to this but no answer that works.
 
Thanks
Fritz
#1

6 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 12079
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/09 05:30:18 (permalink)
    +1 (1)
    fsonnichsen
    XC16 Version 1.6.0
     
    Can someone tell me the appropriate format specifiers for sprintf with:
       float ADC;
     long double ADC;
     
    Using type %f for both I get different results and both always result in a error messages:
       main.c:93:5: warning: format '%g' expects type 'double', but argument 3 has type 'long double'
     main.c:105:5: warning: format '%f' expects type 'double', but argument 4 has type 'float'
     
    I could find nothing in the X16 manual. I tried a few other things. Looking on this forum there are some allusions to this but no answer that works.

    For XC16, both float and double types are 32-bit; however, double is equivalent to the 64-bit long double if -fno-short-double is used.  

    For XC16, the format specifiers %f and %g accept a double, and %Lf and %Lg accept a long double. There's no specifier for float, so maybe use a cast or just don't use float?. ;)
    #2
    Mysil
    Super Member
    • Total Posts : 4111
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/09 05:33:20 (permalink)
    +1 (1)
    Hi,
     
    There is a type modifier that may be inserted before the format character, like "%hi"
    https://www.gnu.org/software/libc/manual/html_node/Integer-Conversions.html
     
    For floating point formatting, there is a type modifier L for long double type:
    https://www.gnu.org/software/libc/manual/html_node/Floating_002dPoint-Conversions.html
     
        Mysil
    #3
    fsonnichsen
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/09 12:32:01 (permalink)
    0
    OK--it worked----I have it in my notes. 
     
    Appreciate it
    Fritz
    #4
    mlp
    boots too small
    • Total Posts : 1005
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/11 20:31:37 (permalink)
    +3 (3)
    1and0
    There's no specifier for float

    because when being passed to a variadic function (like the *printf() family), a float is automatically promoted to double, and has been since 1989. It is physically impossible for *printf() to receive a float.
     
    If your variable is a float, give *printf() the same format string as you would if it was a double.

    Mark (this opinion available for hire)
    #5
    ric
    Super Member
    • Total Posts : 29860
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/11 21:20:20 (permalink)
    +1 (1)
    mark.pappin
    ...
    If your variable is a float, give *printf() the same format string as you would if it was a double.

    How could they then get this error?

    main.c:105:5: warning: format '%f' expects type 'double', but argument 4 has type 'float'

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    mlp
    boots too small
    • Total Posts : 1005
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Format Specfiers for long double and float 2021/01/15 13:39:03 (permalink)
    +2 (2)
    ric
    mark.pappin
    ...
    If your variable is a float, give *printf() the same format string as you would if it was a double.

    How could they then get this error?

    main.c:105:5: warning: format '%f' expects type 'double', but argument 4 has type 'float'


    GCC's parser not knowing the C standard, maybe?
    The (upstream) GCC team has long shown disdain for the language standard, so this would not be a surprise.

    Mark (this opinion available for hire)
    #7
    Jump to:
    © 2021 APG vNext Commercial Version 4.5