2012-01-26 130 views
4

计划概要(3体问题):错误的C代码:预期标识符或“(”前“{”令牌

#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) 
    { 
    ... 
    } 

    ... 

} 

我上线“{/ *分机功能的错误找AX,AY在x和y * /”的不同范围的说法"error: expected identifier or '(' before '{' token"

我认为这可能是由于没有电话或以正确的方式

+0

你的评论是错误的, 它应该是/ *分机功能...和***不*** * \分机功能 – pezcode

+1

感谢downvoting和转换我的答案。他在代码中发布了无效注释块,并在同一行中报告了_syntax_错误。在帮助人们之前,我只会三思而后行,所以我不会干涉你对常见问题的解释。 – pezcode

回答

6

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

实际上,在您的示例中有几个语法/概念错误。请清理并澄清你的问题 - 当你这样做时,我会尽量做出更好的回答。

+0

好的谢谢。我不确定在'清理它'有多远,因为我不想粘贴我的整个代码 – user1170443

+0

对不起,以前从未使用过此网站,我如何缩进代码而不必手动放置四个空格? – user1170443

+0

@ user1170443:全选并使用WMD编辑器小部件中的{}按钮。 – sarnold

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 
$ 

希望它有帮助!

相关问题