2012-01-26 114 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 
$ 

希望它有幫助!

相關問題