Does while loop have two Arguments?

我只是一个虾纸丫 提交于 2020-01-13 06:12:35

问题


My ma'am gave me one question to solve. To predict the output of the following code.

#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    printf("Output is : ");
    while (i < 5, j < 10)    // Doubt: how does while accept 2 arguments?? and how it works??
    {
        i++;
        j++;
    }
    printf("%d, %d\n", i, j);
}

I thought it was a syntax error. But when I tried to run, it gave me output.

Output is : 10, 10

But How? Can anyone explain?

But if I remove the first printf statement printf("Output is : "); and run it, my antivirus give me a alert that a Trojan is detected. But how it becomes a Trojan?


回答1:


The comma operator is a binary operator and it evaluates its first operand and discards the result, it then evaluates the second operand and returns this value.

so in your case,

First it will increment i and j upto 5 and discard.
Second it will iterate i and i upto 10 and provide you the result as 10, 10.

you can confirm by using the following code,

while (i < 5, j < 10)    // Doubt: how does while accept 2 arguments?? and how it works??
{
    i++;
    j+ = 2;
}


来源:https://stackoverflow.com/questions/27133845/does-while-loop-have-two-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!