OutputDebugStringA with a float in VS2010

Posted on 2011-10-25
Medium Priority
Last Modified: 2012-05-12
I have the following class that I use for showing output debug messages in VS2010:
     class Logging

              static void LogMessage( const char *pszFormat, ... )
                     static char buffer[1024];                
                     va_list args;
                     va_start (args, pszFormat);
                     vsprintf(buffer, pszFormat, args);


Open in new window

I use it like so:

float kbps = bitrate / 1024;
Logging::LogMessage( "m_AverageBandwidth = %d kbps\n", kbps);

Open in new window

But this just prints out:
m_AverageBandwidth = 0 kbps

How do I use this function with floating point numbers??
Question by:Wanting2LearnMan
  • 3
  • 2
  • 2
  • +3
LVL 46

Accepted Solution

Kent Olsen earned 168 total points
ID: 37027245

change '%d' to '%f' in the format string.  :)


Author Comment

ID: 37027275
I have tried the following:
%f and %ld
and both don't work :(

I still get the same result

Author Comment

ID: 37027279
kbps will be a number like e.g. 43601.000
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

by:Bill Nolan
ID: 37027282
look up sprintf formatting in the MSDN
LVL 86

Assisted Solution

jkr earned 336 total points
ID: 37027303
Apart from using the right format specifier (as Kdo mentioned), you need to ensure that all arithmetic operations are explicitly floating point operations, i.e.
float kbps = (float) bitrate / 1024.0;

Open in new window


Author Comment

ID: 37027398
It took the %f and:
float kbps = (float) bitrate / 1024.0

Can you tell me why I need the line:
float kbps = (float) bitrate / 1024.0???

When I used:
float kbps = bitrate / 1024
and looked at 'kbps' in the debug output window it desplayed what looked like a valid floating point number.
LVL 86

Assisted Solution

jkr earned 336 total points
ID: 37027431
Well, you did not provide the declarations of your variables, and

float kbps = (float) bitrate / 1024.0;

simply does ensure that everything will be treated as floats - if bitrate already was one, that would have done no harm, but using 1024 instead of 1024.0 is plain dangerous, since the latter is an integer.

Assisted Solution

by:Bill Nolan
Bill Nolan earned 164 total points
ID: 37027499
To clarify what jkr is telling you: you will lose data if bitrate is an int, e.g.  The integer in the calculation will cause the loss of the mantissa values.

Although kbps, being a float, will display properly in the output, its value will have lost precision in your operation.  (If, e.g., if you expected it to be 3.9, it will show as 3.0).
LVL 11

Assisted Solution

cup earned 164 total points
ID: 37029227
||I|f you are getting warnings in the compilation, use

float kbps = (float) bitrate / 1024.0F;
LVL 35

Assisted Solution

sarabande earned 168 total points
ID: 37029859
if kbps shows rightly in the debugger, the calculation was correct.

generally, a term a/b would have a float result if one of a or b was float. so bitrate/1024. would be sufficient even if bitrate was defined as int.

the problem with LogMessage in my opinion is that the variable arguments cannot be passed to the sprintf. the vastart and vaends serves to get the arguments in a function but would not put them onto the stack of the sprintf call. as far as i know there is no easy way out from that. you would need to parse the format string to find count and type of arguments and then would need to call sprintf for each argument seperatly in a loop. obviously that makes no sense.

as an alternative for variable formatting a log message i have seen a way using a preprocessor macro and ostringstream. a call to LogMessage would be like

LogMessage("m_AverageBandwidth = " << kbps << " kbps" << std::endl);

Open in new window


and the technical implementation is to put that you passed as argument to the function at the right side of a ostringstream streaming operation << .

tell me, if you were interested.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question