'==' operator for int==float

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • narayan230979
    New Member
    • Sep 2016
    • 3

    #1

    '==' operator for int==float

    main()
    {
    int a=3;
    float b=3.0;
    if(a==b)
    printf("3.0 is equal to 3 in c language");
    else

    printf("it's not\n");
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You can't use operator== on a float. Because of float rounding, you will never be exactly equal.

    Comment

    • narayan230979
      New Member
      • Sep 2016
      • 3

      #3
      thanks for reply.
      u mean o/p should be :-it's not
      right ?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        The issue isn't that you can't use the == operator with floating point operands; the compiler is happy to let you do it.

        The issue is that you shouldn't do it because of the reason @weaknessforcat s gave above.

        The result of your comparison is unpredictable.

        Comment

        Working...