printf not working for float - how can I convert float on consloe just like integers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    printf not working for float - how can I convert float on consloe just like integers?

    I am doing floating point calculation inside C function, with debugger I can see float values in single step, if I want to print them on console, I am using printf, but printf not working for flaot, how can I convert them to int and print on console using printf. I tried to print with %d, but float, but not printing, any idea?.
    Thanks.
  • shabinesh
    New Member
    • Jan 2007
    • 61

    #2
    if you want to print the floating point value to the console you can use %f . if you want to convert floating point to integer just typecast it. I wonder if you were expecting this answer.

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      somehow printf library not printing if I use %f, but if I typecast to int, then it roundsup but I am loosing that truncated value.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Perhaps you should post the code you are using to print, including data types of variables being printed.

        Additionally it would help to know what platform you are working on, some embedded platforms have options to leaving floating point support out of printf.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by tvnaidu
          somehow printf library not printing if I use %f ...
          Do you mean
          • nothing at all is printed;
          • surrounding text is printed but the floating point value isn't;
          • the wrong value is printed.

          We can't help you if we don't know what is happening.

          I second Banfa's request, we will have a better idea what is going on if you gave us a code snippet -- just the printf call and the types of all variables used in it.

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            Are you using a double or float to declare the real number? They are different sizes in C/C++.

            In the past, I found that %f would work for float but not for double. For double I had to use %lf.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by RRick
              Are you using a double or float to declare the real number? They are different sizes in C/C++.

              In the past, I found that %f would work for float but not for double. For double I had to use %lf.
              No that's wrong, in the absence of a prototyped parameter (such as the printf varidac function declaration) floats are automatically promoted to double for parameter passing (same as shorts and chars are promoted to int). %f is for double, %Lf is for long double.

              In this instance printf and scanf format specifiers differ because for scanf %f is for float, %lf for double and %Lf for long double.

              Comment

              • tvnaidu
                Contributor
                • Oct 2009
                • 365

                #8
                Thanks. I am using float only, not double.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Are you still having trouble with this? If so, please post a relevant code snippet that shows your call to printf and the types of all arguments passed to printf; and describe the result you see and how it differs from what you expect.

                  Comment

                  • tvnaidu
                    Contributor
                    • Oct 2009
                    • 365

                    #10
                    Thanks for help. actually I moved all floats to int, earlier I thought I need float, but most of ADC readings are integers, then when I double and do average and do square-root also it is integers.

                    Comment

                    Working...