如何解决预期标识符错误C语言

时间:2018-12-24 15:12:41

标签: c unix ansi-c

即使我的代码是根据ANSI-C标准编写的,我在编译代码时也会遇到两个“预期标识符”错误。 我使用带有-Wall -ansi -pedantic标志的gcc编译器进行了编译。 我知道编译器很旧,但是我的大学要求我使用它。

复制粘贴的错误:

mycomp.c:202:5: error: expected identifier or ‘(’ before ‘return’
     return SUCCESS;
     ^
mycomp.c:203:1: error: expected identifier or ‘(’ before ‘}’ token
     }
     ^
makefile:10: recipe for target 'mycomp.o' failed

我尝试查看代码,但未发现任何语法问题。我尝试按照错误指南进行操作,例如,在“返回”之前添加“(”或在“}”之前添加“(”),但这并没有改变任何内容,但我仍然会收到这些错误。

代码很长而且很乱,但是最后我还是得到一个错误,或者它是:最后一个返回和最后一个}数字。

int main() {  
    char command[SIZE_OF_LINE];
    int commandIndex; int i;
    int numbers[2]; char var[2];

    /* As requested, initializing all complex values to 0 + 0i */
    read_comp(A, NONE, 0 ,0);
    read_comp(B, NONE, 0 ,0);
    read_comp(C, NONE, 0 ,0);
    read_comp(D, NONE, 0 ,0);
    read_comp(E, NONE, 0 ,0);
    read_comp(F, NONE, 0 ,0);

    commandIndex = 0;

    numbers[0] = commandIndex;
    numbers[1] = commandIndex;
    var[0] = '0';
    var[1] = '0';

    for(i = 0; ; i = 0)
        if(!fgets(command, SIZE_OF_LINE, stdin)) {
            i = undefinedCharSkipper(command, i);
            commandIndex = commandIdentifier(command, i);

            if(cmd[commandIndex].func == NULL) {
                fprintf(stderr, "Command does not exist:%s\n", command);
            }

            if(strcmp(cmd[commandIndex].name, "read_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[1] = numberReceiver(command, i);
                }

                /* Running command */
                read_comp(getComplex(var[0]), NONE, numbers[0], numbers[1]);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "print_comp") == 0 ) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                /* Running command */
                print_comp(getComplex(var[0]), NONE, 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "add_comp") == 0 ) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                add_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "sub_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                sub_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_real") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                /* Running command */
                mult_comp_real(getComplex(var[0]), NONE, numbers[0], 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_img") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                /* Running command */
                mult_comp_img(getComplex(var[0]), NONE, 0, numbers[0]);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                mult_comp_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "abs_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                /* Running command */
                abs_comp(getComplex(var[0]), NONE, 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "stop") == 0) {
                stop(NONE, NONE, 0, 0);
                end(command, i);
            }
        }
        else { /* Failed scanning the user's input */
            printf("\n");
            printf("Scanning had failed");
            printf("\n");
        }
    }
    return SUCCESS;
}

预期结果:编译没有错误,因为这是我遇到的仅有2个错误。 实际结果:出现2个错误。

1 个答案:

答案 0 :(得分:3)

仔细查看并正确缩进代码。

在部分

    }                     //----------------------- (1)
    return SUCCESS;        // -----------------------(2)
}                     

您的花括号不匹配。(1)表示main()函数的右括号,因此标记为(2)的部分;您的return SUCCESS; }位于文件范围内,这是不允许的。

更正缩进并注意大括号。