新手:编译时出现C语法错误

时间:2012-07-02 19:14:22

标签: c for-loop comma-operator

    for(int a = 0, b = 1; b < n; a++; b++)
    {
        if (compare(values[a], values[b]))
            counter++;
        else
            {
            int x = values[a];
            values[a] = values[b];
            values[b] = x;
            }
    }

我在第一行[for(int ...]尝试编译时遇到此错误:

helpers.c:68:41: error: expected ')' before ';' token

为什么我需要添加另一个')'?

1 个答案:

答案 0 :(得分:11)

for(int a = 0, b = 1; b < n; a++; b++)
                                ^
                                |
                              problem

您需要在, - 循环的末尾添加逗号(;)而不是分号(for),其中a和{{1} }}:

b

这是comma operator

这两个SO问题可能也会有所帮助:How do I put two increment statements in a C++ 'for' loop?What is the full "for" loop syntax in C (and others in case they are compatible)?