【问题标题】:Satisfying conditions in a ternary operator三元运算符中的满足条件
【发布时间】:2014-10-14 19:27:58
【问题描述】:

您能否告诉我下面代码中的三元 (:?) 运算符打印**** 应该满足什么条件以及+++++++ 应该满足什么条件?:

printf( "%s\n", count % 2 ? "****" : "++++++++" );

【问题讨论】:

  • 你怎么看?您是否尝试过使用不同的 count 值来查看打印的内容?
  • 在发布到 stackoverflow 之前,您是否尝试过亲自回答这个问题?

标签: c printf operator-keyword


【解决方案1】:

% 运算符是模运算符:http://en.wikipedia.org/wiki/Modulo_operation

这部分:

 count % 2 ? "****" : "++++++++"

表示(伪代码)

 if count is not even 
    return ****
 else
    return ++++++++

这部分

printf("%s\n", 'string')

表示(伪代码)

replace %s with string and print with a newline character

把它们放在一起就像这样:

if count is not even 
    print **** with a newline character
else
    print ++++++++ with a newline character

【讨论】:

  • 这实际上是错误的。 2 % 2 将评估为 false。
  • 是的,我正在编辑答案 - 我注意到在我写完答案后
  • @Jonathan 只是出于好奇,你为什么要删除your attempted edit of the question 中的格式?
  • @admdrew - 不知道我有
猜你喜欢
  • 2014-06-19
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2010-10-20
  • 2019-01-18
  • 1970-01-01
相关资源
最近更新 更多