以下内容摘自Writing solid code , 第 115 页。
int strcmp( const char *strLeft, const char *strRight ) { for( NULL; *strLeft == *strRight; strLeft ++ ,strRight ++ ) if( strLeft == ‘\0’ ) return(0); return ( (*strLeft<*strRight)?-1:1 ); }
The moment you use <, or any other operator that use sign information, you force the compiler to generate nonprtable code
它(粗线)是什么意思?这个我知道
- 右移符号整数是不可移植的。
- 比较有符号整数和无符号整数是不可移植的
为什么比较两个有符号整数是不可移植的?
最佳答案
C 中的“char”类型可以是有符号的或无符号的。所以在这里,如果字符串是 {0x81, 0} 和 {0x32, 0},那么如果 chars 有符号,0x81 将被解释为负数(结果将是第一个字符串比较少),如果 chars 是unsigned 它将被解释为正数(因此第二个字符串比较少)。这是不可移植的,因为结果因您使用的编译器而异。
关于c++ - c中的非可移植符号整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463367/