预期的 ')' 在 ';' 之前代币 C

Expected ')' before ';' token C

我正在尝试制作一个程序,打印出 0 到 100 之间所有偶数的 ^2、^4 和 ^(1/3)。这就是我所拥有的。

#include <stdio.h>
#include <math.h>
main(){
   int a, b, c, i;
   for (i=0; i<100; i=i+2;)
      a = (1*1);
      b = (i*i*i*i);
      c = pow(i, (1/3));
      printf("%d, %d, %d", a, b, c);
      return 0;
}

它在第 6 行给我一个错误

error: expected ')' before ';' token.

这是我第一天使用 c,所以我现在真的很困惑。

for 循环应如下所示:

for (initialization; condition; increment/decrement)

你的看起来不像那样。请注意,括号内正好有两个分号。

去掉for语句中i=i+2;

后面的分号
for (i=0; i<100; i=i+2)

此外,您可能需要在 body 周围使用一些大括号。 for 循环只是一个语句。

类似

for (i=0; i<100; i=i+2) {
      a = (1*1);
      b = (i*i*i*i);
      c = pow(i, (1/3));
      printf("%d, %d, %d", a, b, c);
}

编辑

我认为 C 中的 for 语句由括号内的三个部分组成,右括号后面是单个语句或复合语句,即一系列用大括号括起来的语句牙套。

诸如 for (i=0; i < 10; i=i+2) printf(" i = %d\n", i); 之类的语句将打印出数字序列 0、2、4、6、8。这类似于使用 while 语句的一组语句,如:

i=0;
while (i < 10) {
    printf (" i = %d\n", i);
    i=i+2;
}

for 语句括号中的三个部分是可选的,因此您可以得到类似下面的内容,其结构类似于 while 循环

i=0;
for ( ; i < 10; ) {
    printf (" i = %d\n", i);
    i=i+2;
}

您将第一和第三部分移到了 for 语句之外。请注意,由于 for 语句具有复合语句,因此我们必须使用花括号来指示以下语句,每个语句以分号结尾,都是 for 语句的一部分。

我们还可以在 for 循环中使用逗号分隔的语句,例如以下示例:

int i, j, k;
for (i = j = 0, k = 3; i < 10 && k > 1; i++, j+=2) {
    printf (" i = %d j = %d k = %d\n", i, j, k);
}

以上类似于:

int i, j, k;
j = 0;
i = j;
k = 3;
while (i < 10 && k > 1) {
    printf (" i = %d j = %d k = %d\n", i, j, k);
    i++;
    j+=2;
}

您将 for 循环的初始值设定项和测试条件混合在一起。将其更改为如下所示:

for (i=0; i<100; i=i+2) 

for循环语句只需要两个分号来分隔它的三部分。

所以它应该看起来像:

for (i = 0; i < 100; i = i + 2)

注意有一个分号分隔 i=0i < 100

此外,您可以简化 i = i + 2,只需说 i += 2

至于您的其余代码,

c = pow(i, (1/3));

这行不通。在 C 中,1/3 的计算结果为 0,因为 C 编译器将其视为整数除法。您需要使用 1.0/3.0.

你的for循环不正确

for (i=0, i<100; i=i+2;)

应该是这样的

for (i=0; i<100; i=i+2)

正如@Bill Lynch 所说,您的 for 循环需要:

#include <stdio.h>
#include <math.h>
main(){
   int a, b, c, i;
   for (i=0; i<100; i=i+2) // changed comma to a semicolon
   {//add this bracket
       a = (1*1);
       b = (i*i*i*i);
       c = pow(i, (1/3));
       printf("%d, %d, %d", a, b, c);
   }//add this bracket
   return 0;
}

也不要忘记函数的括号。

以下代码显示了针对已发布代码的所有建议更正。

插入注释以解释更改

#include <stdio.h> // printf
#include <math.h>  // pow

int main()  // <-- use valid return type
{
   int a;    // value ^2
   int b;    // value ^4
   double c; // value ^1/3 note: pow() returns a double
   int i;    // loop index

   // the following loop calculates all the request values
   // from 0 through 98. did you want to include '100'?
   for (i=0; i<100; i+=2) // < corrected for statement
   {                      // < added braces so whole code block loops
      a = (i*i);          // < squared value, not 1
      b = (i*i*i*i);
      c = pow( (double)i, (1.0/3.0) ); // < corrected integer divide
      printf("%d, %d, %lf \n", a, b, c); 
                           // properly printed double,
                           // added newline
                           // so output displayed immediately
   } // end for

   return 0;
} // end function: main