1

I am comparing two equal values but why a < 0.7 is true, one more thing i wanted to check the datatype of 0.7 but I don't know how to do this.

void main(){
  float a = 0.7;
    if(a < 0.7){
       printf("c");
    }else{
       printf("c++");
    }
}

Output: c

one more example:

int a = 10;

if(a > 10){
 printf("if block");
}else{
 printf("else block");
}

Output: else block

jazz_razor
  • 79
  • 1
  • 9
  • 4
    compare to `0.7f`, `0.7` is double – pergy May 08 '17 at 11:18
  • 4
    single precision floating point representation of `0.7` is `0.699999988` – user7860670 May 08 '17 at 11:18
  • can i check 0.7 is double in c program. if yes how – jazz_razor May 08 '17 at 11:19
  • 1
    There is compile time error,include semicolon after printf('c++') – bigbounty May 08 '17 at 11:20
  • @pergy why 0.7 is double? is it by default every number in c get double datatype. – jazz_razor May 08 '17 at 11:31
  • c supports double? I dont think we can make a double variable in c. – Sagar Balyan May 08 '17 at 11:36
  • @SagarBalyan how so? Are you sure you are talking about C and not about some my-awesome-hardware subset of C? – grek40 May 08 '17 at 11:39
  • @SagarBalyan your teacher is... scary?! `void` as a datatype, only 4 primitives, ... thats all so messed up. However, 4 primitives is not completely wrong... see https://en.wikipedia.org/wiki/C_data_types *Basic types* section – grek40 May 08 '17 at 11:46
  • @grek40 can u explain other example in my question – jazz_razor May 08 '17 at 11:50
  • @jazz_razor Is there really a need to explain the second example? `10` is in memory without any rounding errors and 10 is not greater than 10 – grek40 May 08 '17 at 11:50
  • @grek40 So shed me some light. How many data types are there in c? and which one are the primitive ones? – Sagar Balyan May 08 '17 at 11:51
  • @grek40 yeah... oh sorry you are correct... – jazz_razor May 08 '17 at 11:51
  • @jazz_razor yes, afaik every literal with decimal point get `double` type unless adding `f` postfix – pergy May 08 '17 at 12:02
  • more precisely, from the standard: "An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double." [See point 6.4.4.2 / 4](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf) – pergy May 08 '17 at 12:12
  • 1
    @pergy Detail; in C, `0.7` is a _constant_. C has 2 _literals_: _string literal_ and _compound literal_. `0.7` is neither of those 2 literals. – chux - Reinstate Monica May 08 '17 at 14:17
  • @chux yes, thx for clarification – pergy May 08 '17 at 14:21
  • @jazz_razor The quickest way to let you know something is amiss is to turn on all compiler warnings. This saves you time. `float a = 0.7f;` may generate "warning: conversion to 'float' alters 'double' constant value [-Wfloat-conversion]" letting you know _something_ is going on. `float a = 0.7f;` would fix that warning. Now `0.7f` and `0.7` at least look different in code - giving you a hint there may be an important difference. – chux - Reinstate Monica May 08 '17 at 14:56
  • @jazz_razor To check the data type, research `_Generic` – chux - Reinstate Monica May 08 '17 at 14:58

0 Answers0