C语言 得到错误:预期标识符或 '(' before '{' 标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19457418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:44:24  来源:igfitidea点击:

getting the error: expected identifier or ‘(’ before ‘{’ token

cidentifier

提问by Samantha Christine

Getting this error : expected identifier or ‘(' before ‘{' tokenon the first bracket after the #includebefore the int main. No clue why! Doing an assignment for an introductory programming course. It's due today so any help would be appreciated!

得到这个错误:expected identifier or ‘(' before ‘{' token在之前的第一个括号#includeint main。不知道为什么!为介绍性编程课程做作业。今天到期了,所以任何帮助将不胜感激!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
{
int main(void);

int cardNum(int firstCard, int secondCard; 
int highLow;
int score;

score = 0;   
srand(time(NULL));

    printf("The current card is a %d\n" ,firstCard(2,14));
    printf("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)");
    scanf("%d" ,highLow);
if cardNum > 1 && cardNum < 11
{        
     printf ("The card is: %d ,secondCard.");
}        
else if cardNum == 11
{
if highLow == 1, && secondCard > firstCard OR highLow == 2, && secondCard < firstCard
    {   
        score = score + 1; 
        printf ("\n You have guessed correctly.");
        printf ("\n Your current score is %d ,score!\n");
        printf("The current card is a ("%d" ,cardOne). \n Will the next card be           higher(1) or lower(2)? (press 0 to quit)");
    }    
else if highLow == 1, && secondCard < firstCard OR highLow == 2, && secondCard > firstCard  
    {
        score = score - 1;
        printf ("The card is: %d ,secondCard.");
        printf ("\n You have guessed incorrectly.");
        printf ("\n Your current score is %d ,score!\n");
        printf ("The current card is a %d ,cardOne."); 
        printf ("\n Will the next card be higher(1) or lower(2)? (press 0 to quit)");
    }
else if secondCard == firstCard
    {
        printf ("\n Matching cards, no change in score");
    }
else if highLow == 0
    {
        printf ("\n Thanks for playing! Your final score is %d, score.");
    }
else
    {
        printf ("\n Incorrect input. Please enter 0, 1 or 2")       
    }
}    
return(0);
}

I initially had it like that with int main(void){ but then I was getting all these errors instead so I changed it to the semicolon and only got that one error.
a3.c: In function ‘main': a3.c:24:5: error: expected declaration specifiers or ‘...' before ‘score' a3.c:25:5: error: expected declaration specifiers or ‘...' before ‘srand' a3.c:27:5: error: expected declaration specifiers or ‘...' before ‘printf' a3.c:28:5: error: expected declaration specifiers or ‘...' before ‘printf' a3.c:29:5: error: expected declaration specifiers or ‘...' before ‘scanf' a3.c:31:5: error: expected declaration specifiers or ‘...' before ‘if' a3.c:82:1: error: expected declaration specifiers or ‘...' before ‘}' token a3.c:82:1: error: expected ‘;', ‘,' or ‘)' before ‘}' token a3.c:82:1: error: expected declaration or statement at end of input a3.c:82:1: warning: control reaches end of non-void function [-Wreturn-type]

我最初用 int main(void){ 就这样,但后来我得到了所有这些错误,所以我把它改成了分号,只得到了一个错误。
a3.c:在函数“main”中:a3.c:24:5:错误:预期声明说明符或“分数”之前的“...” a3.c:25:5:错误:预期声明说明符或“.. .' 在 'srand' a3.c:27:5 之前:错误:在 'printf' 之前是预期的声明说明符或 '...' a3.c:28:5:错误:在 'printf' 之前是预期的声明说明符或 '...' printf' a3.c:29:5: 错误:'scanf' 之前的预期声明说明符或 '...' a3.c:31:5: 错误:'if' a3 之前的预期声明说明符或 '...'。 c:82:1: 错误: 预期声明说明符或 '...' 在 '}' 标记 a3.c:82:1: 错误: 预期有 ';', ',' 或 ')' 在 '}' 标记 a3 之前.c:82:1: 错误: 输入末尾的预期声明或语句 a3.c:82:1: 警告: 控制到达非空函数的结尾 [-Wreturn-type]

回答by ouah

{
int main(void);

should be

应该

int main(void)
{

Then I let you fix the next compilation errors of your program...

然后我让你修复程序的下一个编译错误......

回答by Aditya Vikas Devarapalli

you need to place the opening brace after main, not before it

您需要将左大括号放在 之后main而不是之前

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

int main(void)
{