printf 打印不稳定 [英] Erratic printing by printf

查看:59
本文介绍了printf 打印不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code snippets although quite same but giving different answers

#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 可以进行内部类型转换吗??

Could printf be doing internal typecasting??

推荐答案

签名类型的欠/溢出是未定义的行为,你不应该依赖它.

Under/Overflow of signed types is undefined behavior, you should not rely on it.

这篇关于printf 打印不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆