What is the output of this c code?

theAvinash

theAvinash

@theavinash-DAs260 Oct 26, 2024
#include<stdio.h>
#include<conio.h>
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<stdio.h>
#include<conio.h>
int main()
{

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

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Sep 30, 2012

    Did it compile?
    I should throw exception on int a=0.7;
  • theAvinash

    theAvinash

    @theavinash-DAs260 Sep 30, 2012

    yes dude
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Sep 30, 2012

    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

    @vijaysy-L0Tg5s Sep 30, 2012

    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

    @theavinash-DAs260 Sep 30, 2012

    how you can say in 2nd case a is stored as 0.699999?
  • theAvinash

    theAvinash

    @theavinash-DAs260 Sep 30, 2012

    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

    @simplycoder-NsBEdD Sep 30, 2012

    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

    @grsalvi-7IhIh1 Sep 30, 2012

    1st case is clear
    but for 2nd case a is getting stored as .700000 still why 'not equal' getting printed ?
  • VIJAYSY

    VIJAYSY

    @vijaysy-L0Tg5s Sep 30, 2012

    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

    @sulochana-anand-OrGmKr Sep 30, 2012

    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

    @sulochana-anand-OrGmKr Sep 30, 2012

    its a type of logical error.
  • grsalvi

    grsalvi

    @grsalvi-7IhIh1 Oct 1, 2012

    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

    @vijaysy-L0Tg5s Oct 1, 2012

    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

    @sulochana-anand-OrGmKr Oct 1, 2012

    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

    @theavinash-DAs260 Oct 1, 2012

    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

    @grsalvi-7IhIh1 Oct 2, 2012

    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