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

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

问题描述


  

可能重复:结果
  三元(条件)运算符用C


这是我的老师援助code例子。我没有什么线索总=总* 2 +(N =='1'?1:0); 一样。我认为它乘以2的总用,但什么是问号和 1:0

  INT bcvt(FILE * INFILE){
  焦炭N;
  INT I,总= 0;
  对于(i = 0; I< 32;我++){
    的fscanf(INFILE,%C,&安培; N);
    总=总* 2 +(N =='1'?1:0);
  }
  烧焦假;
  的fscanf(INFILE,%C,&安培;假);
  总回报;
}


解决方案

语句

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

等同于

 如果(N =='1')返回1
否则返回0

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

格式是:

 (?前pression如果真回报,这个值:别人回报,这个值)

Possible Duplicate:
The ternary (conditional) operator in C

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;
}

解决方案

The statement

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

is equivalent to

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

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

the format is:

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

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

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