这是做什么的? [英] What does this do?

查看:18
本文介绍了这是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C中的三元(条件)运算符

这是我的老师帮助下的代码示例.我不知道 total = total*2+ (n=='1'?1:0); 是做什么的.我认为它将总数乘以 2,但是问号和 1:0 比例是​​什么?

This is a code example from my teacher assistance. I don't have a clue what total = total*2+ (n=='1'? 1:0); does. I think it multiply the total with by 2, but what is with the question mark and the 1:0 ratio ?

int bcvt(FILE *infile){
  char n;
  int i, total=0;
  for(i=0; i<32; i++){    
    fscanf(infile, "%c", &n);
    total = total*2+ (n=='1'? 1:0);
  }
  char dummy;
  fscanf(infile, "%c", &dummy);
  return total;
}

推荐答案

声明

(n=='1'? 1:0)

等价于

if ( n == '1' ) return 1
else return 0

所以如果 n 为 '1' 则返回 1,否则返回 0.

So it returns 1 if n is '1' and 0 otherwise.

格式为:

( expression ? if-true-return-this-value : else-return-this-value )

这篇关于这是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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