printf打印不稳定[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了printf打印不稳定[关闭]相关的知识,希望对你有一定的参考价值。

以下代码片段虽然完全相同但给出了不同的答案

#include <stdio.h>
int main() {
  signed char c = -128;
  c = -c;
  printf("%d", c);.  //-128
  return 0;
}

和:

#include <stdio.h>
int main() {
  signed char c = -128;
  printf("%d", -c);.  //expected -128 but got 128
  return 0;
}

printf可以做内部类型转换吗?

答案

签名类型的Under / Overflow是未定义的行为,您不应该依赖它。

以上是关于printf打印不稳定[关闭]的主要内容,如果未能解决你的问题,请参考以下文章