C ++ is a fun trick (help clarify the person). Znachitso so, I have a friend - the second day I explain to him an incomprehensible problem (until now). For example, there is a comparison:

if (123123123 == num) // num - int 

so here The person does not understand the difference between the example shown above and the following:

 if ((int)123123123 == num) 

So, as I explained: 123123123 - the compiler does not know what it is (it's like that 1 can be interpreted as true, and 0 as false). I told him that 123123123 is simply untyped garbage, which the compiler will arbitrarily lead to a type that he likes and on this basis we can get a similar comparison.

 if(true == num) // 

And it turns out that for any num non-zero condition will be true.

People, if anyone has a clearer explanation of what is happening - write :) Well, sooooo need :)

  • four
    Are you sure who correctly explains to whom? The compiler is well aware that 123123123 is a number. Yes, and this "specific style" is the constant ahead ... Modern compilers catch if (a = 0) very well and swear. And assignment there is rarely needed. - KoVadim
  • However, if (123123123 == num) and if (num == 123123123) compared differently :) Well, experts, your opinion? : D - Stanislav Komar
  • What does it mean to compare differently? Is the sequence of machine commands different? What type of num? Test for "differently" in the studio. - avp
  • one
    What compiler? bit rate? and is the code written correctly? And then maybe there is such an if (m == 123123123); {std :: cout << "nonono" << std :: endl; } - KoVadim
  • one
    Well, the correct answer remains the same - PSNL (underground knock does not cure). Example - in the studio! - alexlz

2 answers 2

Unnamed integer constants are reduced either to int, or (if their value exceeds the int limits) to long. Also floating point - float - double. To explicitly indicate that we want to get a long or double, at the end write L: 44L, 464.22L.

There is such a concept - implicit type conversion. This is when in one expression (arithmetic operation, comparison, etc.) there are variables or constants of several types. Reduction is carried out to the most "long" type. In your case, there is essentially no difference between the two expressions, your friend is right.

In the case of bool, one should remember that bool is essentially a type of char (unlike Java, where this type is not simply cast to an integer) and if implicitly cast, it will be cast to 0 or 1. Other types will be bool with an explicit cast.

    Do not fool a person. An integer literal without a suffix can only be converted to an int, long int or long long int, depending on the size, but not to a bool. But in principle, you are right, I (64-bit architecture) the following example displays only "First":

     #include <iostream> int main() { int i = 2147483647; if ((int)66571993087 == i) { std::cout << "First" << std::endl; } if (66571993087 == i) { std::cout << "Second" << std::endl; } } 

    This is due to the fact that 66571993087 does not fit into an int, therefore it has the type long long int. In the first comparison, we reduce it to int, so we lose the most significant digits and get 2147483647. In the second comparison, i is reduced to the long long type.