-2

i'm looking for the exact reason why does this conversion gives up a warning on c++ but not on c. does it connected to the fact that c++ is strongly type and c is weakly type ? is it because the type in c can be determined while running so the compile does not points out a warning ? thank you.

Pelegyair
  • 19
  • 3
  • 2
    It is just because you are **losing** data, the decimals. So for "Obvious Logical Errors" it raises warnings. And I assume you meant Visual Studio. – Khalil Khalaf Jul 19 '16 at 21:33
  • `c is weakly type` What makes you think so?? – dxiv Jul 19 '16 at 21:34
  • Can you give a specific example? `int x = 1.0;` does not give a warning for me for either language by default. What compiler are you using etc.? – Tavian Barnes Jul 19 '16 at 21:34
  • 7
    Please update your question to show us (a) the code that triggers the warning, (b) the exact text of the warning, and (c) the versions of the C and C++ compilers you're using, plus any options you're specifying. – Keith Thompson Jul 19 '16 at 21:36
  • 1
    @dxiv my proffesor – Pelegyair Jul 19 '16 at 21:37
  • *Both* `C` and `C++` are *statically* typed languages. See also: [Is C strongly typed?](http://stackoverflow.com/questions/430182/is-c-strongly-typed). – dxiv Jul 19 '16 at 21:41
  • 2
    In both C and C++, every expression and every object has a well defined type. What makes them "weakly typed" is that there are a lot of implicit conversions -- but there are strict rules for when those implicit conversions are applied. C++ has *slightly* fewer implicit conversions than C. (There is no rigorous definition of the phrases "strongly typed" and "weakly typed", so arguing whether C and/or C++ qualify is likely to be fruitless.) – Keith Thompson Jul 19 '16 at 21:50

1 Answers1

10

The presence or absence of a warning on a conversion from double to int has nothing to do with any difference between C and C++.

A warning (and you didn't tell us what the warning looks like; please update the question with that information) is probably valid. If the truncated double value is outside the representable range of int, the behavior is undefined. If it's within the range, but not mathematically equal to an integer, then the conversion will loose information.

Some compilers will warn about things like this, others won't -- and a given compiler may or may not issue a warning depending on what options you specify.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • thank you for the comment. im using the lastest gcc but my question is theoretical. why don't the c compiler does not show a warning ? – Pelegyair Jul 19 '16 at 21:40
  • 1
    @Pelegyair: There is no theoretical reason; exactly the same considerations apply to C and to C++. You're seeing a symptom (a warning from your C++ compiler but not from your C compiler). Again, please update your question with information about the compilers you're using. – Keith Thompson Jul 19 '16 at 21:44
  • 1
    Minor: "If the double value is outside the representable range of `int`" --> the range is _slightly_ bigger. Example: -2147483648.999... to 2147483647.999... – chux - Reinstate Monica Jul 19 '16 at 21:50