📜 ⬆️ ⬇️

When 2 + 2 = "4"

While relaxing from debugging JavaScript, I read an article about the features of PHP and suddenly wondered what the results of the comparison 2 + 2 = "4" will be for different programming languages. And that's what came out of it.
Summary table for those who do not want to read a lot
ResultCompilation errorFalseTrue
LanguagesC ++
Pascal
FORTRAN-95
Java
C #
Go
Rusty
Haskel
C
Lisp
Python
Lua
Ruby
Javascript
Php
Perl

The C compiler generates a warning, the C ++ compiler error that turns into a warning if you specify the -fpermissive flag.

Under the cut for each of the languages ​​is a line of code that caused one or another result, with a little comment.

Bonus - console output functions for these languages.

Compilation error


The simplest and uninteresting behavior is a compilation error that occurs in all strongly typed languages.
')
C ++

cout << (2 + 2 == "4") << endl; 

error: ISO C ++ forbids comparison between pointer and integer [-fpermissive]

If you specify the -fpermissive flag, a pointer to a string will be compared to the number 4, which will most likely return 0.

Pascal

 writeln((2 + 2) = "4"); 

Fatal: illegal character "" "" ($ 22)
The error message is rather incomprehensible

 writeln(2 + 2 = '4'); 

Error: Incompatible types: got "Char" expected "Int64"
The error message has become quite understandable. Thanks GebekovAS .

FORTRAN-95

 Print *, 2 + 2 == "4" 

Error: Operands of comparison operator '==' at (1) are INTEGER (4) / CHARACTER (1)

It's funny that in this text there is actually no message that comparison is impossible, just a statement of fact.

Java

 System.out.println((2 + 2) == "4"); 

error: incomparable types: int and String

In the error message, everything is fine except that it does not indicate which operation caused the error.

C #

 Console.WriteLine((2 + 2) == "4"); 

error CS0019: Operator `== 'cannot be applied to operands of type` int' and `string '

It's all good.

Go

 fmt.Printf(2 + 2 == "4") 

cannot convert "4" to type int
invalid operation: "4" == 2 + 2 (mismatched types string and int)

And even better

Rusty

 println!("{:?}", 2 + 2 == "4"); 

error: the trait `core :: cmp :: PartialEq <& str>` _` [E0277]

Logical but honestly far from the most understandable error message

Haskel

 main = putStrLn (show ((2 + 2) == "4")) 

No instance for (Num Char) arising from a use of `+ '

At first I did not understand the message, then I thought and understood a little. But Haskel began to understand even less than before.
Sirikid solution if you still really want to compare ideone.com/CJifV3

False result


The next in line languages ​​that allow you to compare the number and string, while returning false (or something similar), most likely due to the mismatch of the types of variables.

C

 printf("%i\n",(2 + 2 == "4")); 

the result is 0. Comparison of the pointer to the string and the number 4 will most likely return 0. This displays a warning, almost the same as in C ++:

warning: comparison between pointer and integer [enabled by default]

Lisp

 (write (eq "123" 123)) 

result - NIL

Python

 print (2 + 2 == "4") 

the result is False

Lua

 print(2 + 2 == "4") 

false result

Ruby

 puts 2 + 2 == "4"; 

false result

1C
 2+24» 

False
Thank you, ACPrikh . "Adineesnegi joined the ranks of C Lisp Python gurus Lua Ruby."

True result


Finally, languages ​​that return true for comparison.

Javascript

 console.log(2 + 2 == "4"); 

Fortunately, there is a strict comparison operation (triple equality ===), which returns false

Php

 echo (2 + 2 == "4"); 

Just as in JavaScript, there is a triple equality operation, the result of which is FALSE, which looks like an empty line when outputting to the console (if I confuse me, correct me). Me).

Perl

 print 2 + 2 == "4"; 

There is no strict comparison, besides for hexadecimal notation (and why not)

 print 0x2 + 0x2 == "0x4"; 

Perl interprets the string “0x4” as 0 (as opposed to JavaScript and PHP, which parse hexadecimal strings) and returns FALSE, and the console will also have an empty string.

Rexx
 say 2+2="4" 

true
Thank you, impetus . "With the default keys / settings, which is documented and for this language is logical and expected."

Tcl
 puts [expr { 2 + 2 == "4" }] 

true
Thanks Chpock , "every value is a string".

Are there any other languages ​​with unusual behavior when comparing numbers with strings, I don't know yet.

To study the issue used the site , used versions of compilers and interpreters correspond to those provided on the site.

UPDATE
Fixed an annoying bug with pascal, thanks for pointing. And added examples from the comments.

Source: https://habr.com/ru/post/330176/


All Articles