c代码中的错误:预期标识符或'('''''''之前的标记

时间:2012-01-26 01:42:35

标签: c function error-handling compiler-errors

计划简介(3身体问题):

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double ax, ay, t;
double dt;
/* other declarations including file output, N and 6 command line arguments */
...

int main(int argc, char *argv[])
{
  int validinput;
  ...
  /* input validation */

  output = fopen("..", "w");
  ...
  /* output validation */

  for(i=0; i<=N; i++)
  {
    t = t + dt;
    vx = ...
    x = ...
    vy = ...
    y = ...
    fprintf(output, "%lf %lf %lf\n", t, x, y);
  }

  fclose (output);

}

/* ext function to find ax, ay at different ranges of x and y */
{ 
  declarations

  if(x < 1)
  {
    ax = ...
  }

  else if(x==1)
  {
    ax = ...
  }
  ...
  else
  {
    ...
  }

  if(y<0)
  {
    ...
  }

  ...

}

我在'{/ * ext函数找到ax,ay在不同的x和y * /'范围内得到错误"error: expected identifier or '(' before '{' token"

我认为可能是因为没有以正确的方式调用或创建外部函数

2 个答案:

答案 0 :(得分:6)

你的功能需要一个名字!任何函数之外的代码块在C中都是没有意义的。

事实上,您的示例中存在一些语法/概念错误。请清理它并澄清你的问题 - 当你这样做时我会尽力回答。

答案 1 :(得分:5)

现在,让我们举几个例子。

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("hello world \n");
    return 0;
}

{
    printf("do you see this?!\n");
}

如果您编译上述程序,它将给您以下错误

$ gcc q.c 
q.c:10:1: error: expected identifier or ‘(’ before ‘{’ token
$ 

这是因为gcc编译器在identifier之前需要{。所以我们需要更新上面的程序如下

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("hello world \n");
    return 0;
}

void function()
{
    printf("do you see this?!\n");
    return;
}

它会正常工作。

$ gcc q.c 
$ ./a.out 
hello world 
$ 

希望它有所帮助!