满足三元运算符的条件

时间:2014-10-14 19:27:59

标签: c printf operator-keyword

你能否告诉我下面的代码中三元(:?)运算符应该满足哪些条件来打印****以及+++++++的内容?:

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

1 个答案:

答案 0 :(得分:0)

%运算符是模运算符: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