473,748 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

365 Contributor
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 5121
shabinesh
61 New Member
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 Contributor
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 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
@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 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
@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 Contributor
Thanks. I am using float only, not double.
Nov 13 '09 #8
donbock
2,426 Recognized Expert Top Contributor
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 Contributor
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
2088
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
146698
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 what I wanted. Preferably the varchar would display the number to 2 decimal places but I'd settle for integers only as this conversion isn't business critical and is a nice to have for background information. Casting to MONEY or NUMERIC before...
7
1700
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 to display the .00 even if it does not have a .00 value like this if Var is 50, i need to display .50 instead of just .5
5
2331
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
3922
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 to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
17
2512
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
11
15193
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 the caller of the function to have to pass floats instead of integers. How do I convert the arguments passed to the function into floats before I do the division? Is this necessary, or is their a better way? Thanks, Scott Huey
29
11105
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
2771
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8252
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.