C语言 预期的声明说明符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19458619/
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
expected declaration specifier error
提问by Samantha Christine
I am in intro programming and struggling with this assignment any help is appreciated! Here is the code:
我正在从事入门编程,正在为这项任务苦苦挣扎,任何帮助表示赞赏!这是代码:
#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)
{
printf ("The card is: Hyman");
}
else if (cardNum == 12)
{
printf ("The card is: Queen");
}
else if (cardNum == 13)
{
printf ("The card is: King");
}
else if cardNum == 14)
{
printf ("The card is: Ace");
}
{
if (highLow == 1 && secondCard > firstCard) || (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) || (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);
}
These are the errors:
这些是错误:
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:52:9: error: ‘highLow' undeclared (first use in this function)
a3.c:52:9: note: each undeclared identifier is reported only once for each function it appears in
a3.c:52:25: error: ‘secondCard' undeclared (first use in this function)
a3.c:52:38: error: ‘firstCard' undeclared (first use in this function)
a3.c:52:49: error: expected expression before ‘||' token
I know about the undeclared part because I was supposed to make random numbers and store them but I still havent quite figure that out (any advice would be awesome) but mainly I am concerned about the extensive list of expected declaration specifiers. Any help would be appreciated!
我知道未声明的部分,因为我应该制作随机数并存储它们,但我仍然没有弄清楚(任何建议都会很棒),但主要是我担心预期声明说明符的广泛列表。任何帮助,将不胜感激!
回答by Ashish
1)int cardNum(int firstCard, int secondCard;
1)int cardNum(int firstCard, int secondCard;
Instead of this you should write
而不是这个你应该写
int cardNum, int firstCard, int secondCard;
int cardNum, int firstCard, int secondCard;
because you are declaring variables
因为你在声明变量
2)if (highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard)
2)if (highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard)
instead of this write statement given below
而不是下面给出的这个写语句
if (highLow == 1 && secondCard > firstCard) || (highLow == 2 && secondCard < firstCard)
if (highLow == 1 && secondCard > firstCard) || (highLow == 2 && secondCard < firstCard)
3)else if (highLow == 1, && secondCard < firstCard) || (highLow == 2, && secondCard > firstCard)
3)else if (highLow == 1, && secondCard < firstCard) || (highLow == 2, && secondCard > firstCard)
you should write
你应该写
else if (highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard)
else if (highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard)
回答by hek2mgl
Look at this line:
看看这一行:
int cardNum(int firstCard, int secondCard;
I guess it should be:
我想应该是:
int cardNum, int firstCard, int secondCard;
Also the if statements are wrong. I guess it should be:
if 语句也是错误的。我想应该是:
if ((highLow == 1 && secondCard > firstCard) || (highLow == 2, && secondCard < firstCard))
and
和
else if ((highLow == 1 && secondCard < firstCard) || (highLow == 2 && secondCard > firstCard))
You should start learning C from scratch.
你应该从头开始学习 C。
回答by Ashish
#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)
{
printf ("The card is: Hyman");
}
else if (cardNum == 12)
{
printf ("The card is: Queen");
}
else if (cardNum == 13)
{
printf ("The card is: King");
}
else if cardNum == 14)
{
printf ("The card is: Ace");
}
{
if ((highLow == 1 && secondCard > firstCard) || (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) || (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);
}
回答by Ivan Aksamentov - Drop
Just use any Integrated development environment (IDE)with syntactic errors highlighting. We do not want to be your editors.
只需使用任何带有语法错误突出显示的集成开发环境 (IDE)。我们不想成为你们的编辑。
Here is some useful links (obviously any C++ IDE will check C syntax too):
这是一些有用的链接(显然任何 C++ IDE 也会检查 C 语法):
Happy coding!
快乐编码!

