473,403 Members | 2,323 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,403 software developers and data experts.

What is problem in type declaration FLOAT?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main ()
  4. {
  5. float a;
  6. clrscr();
  7. a=11/5;
  8. printf ("%f",a);
  9. getch ();
  10. }
Output: 2.000000
Since "a" is defined as float it should have given "2.2" output but gives an integer value.
WHATS WRONG?
Oct 20 '13 #1
5 1217
use the value a is either in any format given 11.0/5.0 or 11.0/5 or 11/5.o so you will get the correct answer
Oct 20 '13 #2
@JATINCSE cant i do it without putting ".0" behind ? is it compulsory?
Oct 20 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
You a mixing integer and floating point division:

Expand|Select|Wrap|Line Numbers
  1. a = 11/5;
11/5 is 2. Integers have no decimal portion.

Considering the variable is a float you would code:

Expand|Select|Wrap|Line Numbers
  1. a = 11.0f/5.0f;
If you code:
Expand|Select|Wrap|Line Numbers
  1. a = 11.0/5.0;
you will get a warning from the compiler. The 11.0/5.0 is a double and then you assign the double to a float, which is smaller, so you get a warning about possible data loss.
Oct 20 '13 #4
donbock
2,426 Expert 2GB
@sauravamatyaw:
The compiler first evaluates the expression on the right side of the equals sign and then assigns the computed value to the variable on the left side of the equals sign. The compiler doesn't notice the type of the left-side variable until the assignment stage; but by then it is too late -- the expression has already been evaluated using integer math.
Oct 21 '13 #5
@sauravamatyaw:
i think so because it is depend upon the working of the compliler
first it evaluates the expression on right side as on right it is the ratio of two integers so compiler will evaluate in terms of int then assign its value to float
Oct 23 '13 #6

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

Similar topics

3
by: Marcus | last post by:
I have a column in a MySQL database called price and of type FLOAT(8,2). I then insert two rows into my table, one where price = 100.00 and one where price = 100.01, and then try to find these...
3
by: Tee | last post by:
Hi guys, I have a problem with my VS project, it keeps saying COM Interop registration failed. Could not find a type library for assembly 'Class1'. If I choose to continue, everything still work...
3
by: Steven T. Hatton | last post by:
Somewhere I seem to recall an example where the author was able to access some kind of representation of a variable's type and use it to create another variable of the same type. It may have been...
2
by: MFRASER | last post by:
How can I tell what object type is dropped onto my control? Here is a snippet of code. private void lvwTasks_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { //I want to...
12
by: JC | last post by:
Hi... Anybody knows what problem has this code? I think, in the Garbage Collector? You know the Solution? The program in the test's case, whit 350 contacts, run OK before number 86. The error...
1
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to understand namespaces and assemblies. How are they related and what does type mean in namespaces and assemblies?
1
by: kumarkishor | last post by:
Hi Since last 1 month i have been studying asp .The question comes that what is type of server object and also the class defination of server class.
3
by: burtoncn | last post by:
Hi, Just a little background information on the project I am working on! I am responsible for creating a database to automatically generate travel claims for conference attendees (Approx. 300)...
2
by: mohit123 | last post by:
it shows too many type declaration in the void push function. and i use turbo c++.. --------------------- #include<stdio.h> #define size 5 struct tack {int top; int item ; }
3
by: annaaaa | last post by:
okay for instance int x = 4; double y = 2; printf("%d", (x / y + 5)); what data type is (x / y + 5) ?? thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.