Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6364687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:11:49+00:00 2026-05-25T00:11:49+00:00

C++: What is the printf() format spec for float ? (Visual C++) It used

  • 0

C++: What is the printf() format spec for float? (Visual C++)

It used to be that I used %g for float and %lg for double.

It looks like the spec changed and float is undefined and double is %g.

I have bits in memory that I am printing out so casting is not an option.

Is there a way that I can print out float values using printf() ?

Update:

This code was written for unit testing generic C++ libs used on an embedded system.
Here’s what I had to do to get the float to work.
The code is in a template function:

template <typename T,typename TTYP,typename Ttyp,int bits,bool IsSigned> 
Error testMatrixT() 
{ ...

Here is a code snip:

if (typeid(Ttyp) == typeid(float)) {    
    float64 c = *(float32*)&Tp(row,col);
    float64 a1 = *(float32*)&Arg1(row,col);
    float64 a2 = *(float32*)&Arg2(row,col);
    float64 e = *(float32*)&Exp(row,col);
    m_b = (c == e);
    _snprintf(m_acDiag, sizeof(m_acDiag)-1
        , "add(Arg1,Arg2): arg1=%g, arg2=%g, Expected=%g, Actual=%g, Result: %s"
        , a1, a2, e, c, BOOL_PF(m_b));
} else {
    ...

Pretty ugly isn’t it? Using floats as args give bad output. Maybe due to using _snprintf() ?
Years ago I would use %lg and it would be OK. Not anymore.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:11:50+00:00Added an answer on May 25, 2026 at 12:11 am

    double and float use the same format specifiers with printf (%a, %e, %f, and %g). This is because printf is a variadic function. Any float arguments are implicitly promoted to double before the call; you can’t actually pass a float to printf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While researching how to do cross-platform printf() format strings in C (that is, taking
With GCC, I can specify __attribute__((format(printf, 1, 2))) , telling the compiler that this
In C, I'd like to use printf to display pointers, and so that they
I've always used printf , and I've never used write / format . Is
in my c++ linux application I have this macro: #define PRINT(format,arg...) printf(format,##arg) I want
I keep stumbling on the format specifiers for the printf() family of functions. What
When printing RFC documents on my A4 format printer I noticed that I print
I have the following preprocessor divective: #ifndef NDEBUG #define TRACE printf #else #define TRACE(...)
NSLog function accepts printf format specifiers. My question is about %x specifier. Does this
I'm trying to write a function that accepts a variable number of parameters like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.