尽管更安全的编程语言倾向于阻止字符串和整数比较,仍然有语言希望让它"合法".
- C/ObjC 语言允许字符串和整数比较大小,编译器虽然会提示警告,依然改变不了编译器认为字符串地址也是一个数值,是可以和整数比较大小的语言设计原则。
- warning: comparison between pointer and integer
- C++ 和C语言不同,不允许char *字符串和整数比较大小:
error: ISO C++ forbids comparison between pointer and integer
也不允许string类型和整数比较大小,会有如下错误:
error: no match for 'operator>' (operand types are 'std::string' {aka 'std::basic_string<char>'} and 'int') - Python 不允许字符串和整数比较大小或算术运算:
TypeError: 'xxx' not supported between instances of 'str' and 'int'- age = 21 代表age是整数,age = "21"代表age是字符串