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

what is the reason of giving output is ravi except rahul

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. float p=0.7;
  6. if(p==0.7)
  7. printf("rahul");
  8. else
  9. printf("ravi");
output: ravi

why????
Aug 31 '10 #1
3 1108
newb16
687 512MB
0.7 seems to be a popular number - http://bytes.com/topic/c/answers/894...tation-numbers
Aug 31 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
You cannot use the == operator with floating point.

Due to rounding, numbers that are close in value may test equal.

Ditto for !=.
Sep 2 '10 #3
haicp
4
In this kind of comaprisions, 99% of the time the else part of the if condition gets executed.

In this example, when you try to compare 0.7 with 0.7, what actually happens internally is, one of the 0.7 is interpreted as 0.6999999999.

Floating point representation is machine dependent.

Even though there is not much of a difference between 0.7 and 0.6999999999, the if condition fails.

So, never use floating numbers for comparision.
Sep 3 '10 #4

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

Similar topics

5
by: Tushar | last post by:
hello all, i would like to know why this code compiles and gives the output Code 1: ---------- int main(){ printf("String" + 2); }
5
by: Christopher Benson-Manica | last post by:
Can you correctly identify the output of the following program, without compiling it? #include <stdio.h> int main( void ) { int a=1; int b=0; int foo=10+a?1:0+b?1:0;
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
3
by: bagmita | last post by:
int makeArray( int size) { int triArray = new int ; int val=1; for( int i = 0; i < triArray.length; i++ ) { triArray = new int; for( int j=0; j < triArray.length; j++ ) { ...
18
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
7
by: locy | last post by:
what is the output of the the following code,if the user give"today is thursday" ? const int LENGTH = 18; char message_; std::cout<<"which day is today?"<<std::endl; std::cin>>message_;...
1
by: ashokakr | last post by:
Hi all declare cnt integer; begin cnt=5; dbms_output.put_line(cnt); end;
46
by: Kenny O'Clock | last post by:
This came up in a job interview, what is the output of the program below? I tried to compile and run it myself, but my compiler (lcc-win32) aborts with this errors.... Warning test2.c: 3 ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...
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.