473,412 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

365 100+
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.
Nov 12 '09 #1
9 5094
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.
Nov 12 '09 #2
tvnaidu
365 100+
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.
Nov 12 '09 #3
Banfa
9,065 Expert Mod 8TB
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.
Nov 12 '09 #4
donbock
2,426 Expert 2GB
@tvnaidu
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.
Nov 12 '09 #5
RRick
463 Expert 256MB
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.
Nov 13 '09 #6
Banfa
9,065 Expert Mod 8TB
@RRick
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.
Nov 13 '09 #7
tvnaidu
365 100+
Thanks. I am using float only, not double.
Nov 13 '09 #8
donbock
2,426 Expert 2GB
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.
Nov 13 '09 #9
tvnaidu
365 100+
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.
Nov 15 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
3
by: David Sharp | last post by:
I'm trying to find a way to format a FLOAT variable into a varchar in SQL Server 2000 but using CAST/CONVERT I can only get scientific notation i.e. 1e+006 instead of 1000000 which isn't really...
7
by: nephish | last post by:
Hullo all ! i have a real easy one here that isn't in my book. i have a int number that i want to divide by 100 and display to two decimal places. like this float(int(Var)/100) but i need it...
5
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
11
by: redefined.horizons | last post by:
I'm still pretty new to Python. I'm writing a function that accepts thre integers as arguments. I need to divide the first integer by te second integer, and get a float as a result. I don't want...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.