错误:预期的声明说明符或' ...'之前' {'代币

时间:2015-01-04 20:25:19

标签: c

我一直试图从一本名为C Programming Absolute Beginner's Guide的书中学习C,但我遇到了一个问题,唯一不好的是你不能向一本书提问!在Google中输入错误,它将我带到了这个网站。我得到的错误在问题标题中,这是我的代码。

#include <stdio.h>

 main(

{

 //Set up the variables, as well as define a few

    char firstInitial, middleInitial;
    int number_of_pencils;
    int number_of_notebooks;
    float pencils = 0.23;
    float notebooks = 2.89;
    float lunchbox = 4.99;


    //The information for the first child
    firstInitial = 'J';
    middleInitial = 'R';

    number_of_pencils = 7;
    number_of_notebooks = 4;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2f\n\n", number_of_pencils*pencils + number_of_notebooks*notebooks + lunchbox);

    //The information for the second child
    firstInitial ='A';
    middleInitial = 'J';

    number_of_pencils = 10;
    number_of_notebooks = 3;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2f\n\n", number_of_pencils*pencills
        + number_of_notebooks*notebooks + lunchbox);

    //The information for the third child
    firstInitial = 'M';
    middleInitial = 'T';

    number_of_pencils = 9;
    number_of_notebooks = 2;

    printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
        firstInitial, middleInitial,number_of_pencils,
        number_of_notebooks);
    printf("The total cost is £%.2f\n",
        number_of_pencils8pencils + number_of_notebooks*notebooks + lunchbox);

        return0;
}

)

此代码有什么问题?

2 个答案:

答案 0 :(得分:2)

你的主要功能不好。编译器说它。

应该看起来像

main()
{
....
}

而不是

main(
{
...
}
)

答案 1 :(得分:1)

您的main()功能开始了:

main(
{

并结束:

}
)

这是错误的。它应该是:

int main(void)
{
    …body of function…
}

void是可选的。现代C中的返回类型不是可选的(C89 / C90标准允许它是可选的; C99及更高版本需要它;即使你的编译器不坚持它,你应该编程就好像它是必需的)。正确的返回类型为int(有关详细信息,请参阅What should main return in C and C++?。)

此外,作为Rizier123 pointed out,在return0;的末尾有main();那应该是return 0;

我没有编译代码以查看其他错误,但是括号和括号的错误处理是导致初始错误的原因。