Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
while(scanf(";%d";,&;n),n)是什么意思?_C_While Loop_Conditional Statements_Operators - Fatal编程技术网

while(scanf(";%d";,&;n),n)是什么意思?

while(scanf(";%d";,&;n),n)是什么意思?,c,while-loop,conditional-statements,operators,C,While Loop,Conditional Statements,Operators,给定以下代码: // this is a solution of uva 12279 #include<stdio.h> int main() { int arr[10000],i,n,a,d=0,e=75; while(scanf("%d",&n),n)// what's that means? { d++; int c=0,b=0; if(n==0) return

给定以下代码:

// this is a solution of uva 12279    
#include<stdio.h>

int main()
{
    int arr[10000],i,n,a,d=0,e=75;
    while(scanf("%d",&n),n)// what's that means?
    {
        d++;
        int c=0,b=0;

        if(n==0)
            return 0;

        for(i=0;i<n;i++)
            scanf("%d",&arr[i]);

        for(i=0;i<n;i++)//this is for loop
        {
            if(arr[i]==0)
                c++;
            else
                b++;
        }
        a=b-c;

       printf("Case %d: %d\n",d,a);
  }
  return 0;
}
//这是uva 12279的解决方案
#包括
int main()
{
int-arr[10000],i,n,a,d=0,e=75;
while(scanf(“%d”,&n),n)//这是什么意思?
{
d++;
int c=0,b=0;
如果(n==0)
返回0;
在这种情况下(i=0;i)

while(scanf("%d",&n),n)
这里使用了所谓的逗号运算符

scanf("%d",&n),n
该条件的值是变量
n
的逗号运算符的第二个操作数的值。如果
n
不等于0,则执行循环

你可以这样想象

start: scanf("%d",&n);
if ( n != 0 )
{
    //...
    goto start;
}
while ( scanf("%d",&n) == 1 && n != 0 )
因此,在循环体中,变量
n
不能等于0,除非重新赋值

if(n==0)
return 0;
没有道理

用下面的方法写循环的条件会更正确

start: scanf("%d",&n);
if ( n != 0 )
{
    //...
    goto start;
}
while ( scanf("%d",&n) == 1 && n != 0 )
来自C标准(6.5.17逗号运算符)

2逗号运算符的左操作数计算为空 表达式;在其求值和该表达式之间有一个序列点 然后对右操作数求值;结果 有它的类型和价值

scanf()
返回
EOF
n
是谁知道是什么时,
while(scanf(“%d”)和&n),n)的可能重复也可能是一个无限循环。这是糟糕的代码。