What is the output of this c code?

#include
#include
int main()
{

int a=0.7;
clrscr();
if(a==0.7)
printf("equal");
else
printf("not equal");
getch();
}
.....................................
make required changes to run on linux...
also answer this
#include
#include
int main()
{

float a=0.7;
clrscr();
if(a==0.7)
printf("equal");
else
printf("not equal");
getch();
}

Replies

  • simplycoder
    simplycoder
    Did it compile?
    I should throw exception on int a=0.7;
  • theAvinash
    theAvinash
    yes dude
  • simplycoder
    simplycoder
    What compiler are you using?
    Prefer DevC++ or Code blocks.(See the list of compilers they are using.)
    Please donot use Turbo C/C++.

    As well, conio is not a standard c library function.
  • VIJAYSY
    VIJAYSY
    out put is -not equal
    because in first case data type of a is int so,a is stored as 0;
    in second case even data type of a is float,but internally a is stored as 0.699999
  • theAvinash
    theAvinash
    how you can say in 2nd case a is stored as 0.699999?
  • theAvinash
    theAvinash
    in 2 nd case.
    i think 0.7 is treated as double..so the if condition fails...
    if we type cast 0.7 to float as (float)0.7.... the answer is equl
  • simplycoder
    simplycoder
    Avinash Waghole
    in 2 nd case.
    i think 0.7 is treated as double..so the if condition fails...
    if we type cast 0.7 to float as (float)0.7.... the answer is equl
    This can be justified by the computer organization-architecture.
    Read how floats are saved in computer. You would also require some knowledge on mantissa-exponent form.
    Try to represent 0.7 in binary, you would get the answer.
  • grsalvi
    grsalvi
    1st case is clear
    but for 2nd case a is getting stored as .700000 still why 'not equal' getting printed ?
  • VIJAYSY
    VIJAYSY
    yes a is stored as 0.700000
    (up to my knowledge)
    the 0.7 which is used to compare with 'a' is taken as double
    you can check with sizeof(0.7) it shows 8.
    if you type cast 0.7 to float it will convert to float ,
    the data type of 0.7(which is used to compare with a) is not in hands of user,
  • sulochana anand
    sulochana anand
    the answer is not equal.because int data type only stores without decimal values.so it will not undetand first part and controll will move to else part.
  • sulochana anand
    sulochana anand
    its a type of logical error.
  • grsalvi
    grsalvi
    Okay...
    But suppose float a=0.7; (compiler stores : a=0.700000)
    And double b=0.7; (compiler stores : b=0.70000000000000)

    Still they should be equal.And why difference between a and b is not zero?
    Any idea what way the difference is getting calculated?
  • VIJAYSY
    VIJAYSY
    i guess,
    out put is 0.000000 ,0,or 0.00000000000000 depends on format specifiers used in printf function ,while subtraction implicit type conversation will take place ,a is converted to double,
  • sulochana anand
    sulochana anand
    we know that when we declare any data type our system allocate space in memory accordingly.double and float both support decimal values.but only size is diffrent.float occupy 4 bytes and double 8 bytes.if we would store value which eceeds 4 byte in float it will give an error.but 0.7 is limited value so it will give right answer.
  • theAvinash
    theAvinash
    Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..

    The float function takes care of this problem -
    float(0.7)
    it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.

    its good to use the float function...
  • grsalvi
    grsalvi
    Avinash Waghole
    Computers use a binary (0's and 1's) system to store decimal numbers. This leads to some inaccuracy, since some decimal values can't be stored exactly in binary....0.7 is stored as the value 0.69999999.Since that value isn't 0.7, the condition fails..

    The float function takes care of this problem -
    float(0.7)
    it rounds the value 0.69999999 to 0.7. Many decimal values are stored accurately in binary, for example 0.5, but many are not.

    its good to use the float function...
    I told above that there is no problem during storage as seen during debugging.
    The problem is difference between a and b is not zero,hence machine says they are unequal.

    see below,

    float a=0.7 (compiler stores 0.700000)
    double b=0.7 (compiler stores 0.70000000000000)

    During debugging : a-b=-9.2559631349318e+061

You are reading an archived discussion.

Related Posts

Why java is/isn't pure object oriented?...give smart ans...
I think we've the world's best way of answering true / false type of questions. Here we go, share your opinion - Would you call it a 'crazy engineer' way...
So, here is the scenario. I have a csv (comma separated values) file with almost 40 lakh records in it. Each record is separated by a new line i.e. each...
sir,, can any one post about hacking. I am mechanical student. Bt i am more intrested to know about hacking. If their any videos please post it... Or their any...
hello sir, with great regards and respects i am requesting u to gives me best project based on ccna so that i could submit it to my college as training...