【问题标题】:Error: expected declaration specifiers or '...' before '{' token错误:“{”标记之前的预期声明说明符或“...”
【发布时间】:2015-01-04 20:25:18
【问题描述】:

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

#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;
}

)

这段代码有什么问题?

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快阅读About 页面。当您遇到编译错误时,最好包含确切的错误消息 - 除了如果文件名是一英里长的绝对路径,请将名称缩减为文件的基本名称(因此将 /huge/long/path/with/many/levels/of/directory/src/file.o 更改为 @ 987654326@)。确保您显示的源代码和其中的行号与编译器所说的一致。如果文件太长而不方便,请创建一个 MCVE (How to create a Minimal, Complete, and Verifiable Example?)
  • 我认为最后一个字符 ) 应该在 main( 之后所以 -> main() 这对你有用吗? (顺便说一句:return0; -> return 0;
  • 我查看了本书的示例材料,并且(在第 2 章中)示例程序都有main() 而不是int main()(或者,我更喜欢int main(void)) .它们确实包含必要的return 0;,因为它们没有使用允许省略return 0; 的C99 表示法(尽管我也不喜欢这样做的代码)。第 3 版的日期为 2014 年;令人担忧的是,第一个程序已经有 15 年没有用当前的 C 语言编写了。我不喜欢将returnwhileintiffloat 描述为“命令”——它们是关键字!当心!
  • @Dave:请将一个答案标记为解决方案。如果可以:删除您的“谢谢回答”,因为这不能回答问题。

标签: c


【解决方案1】:

你的main函数不好。编译器说的。

应该是这样的

main()
{
....
}

代替

main(
{
...
}
)

【讨论】:

  • 在现代 C(自 2000 年以来编写的任何东西)中,main() 函数应该具有明确的返回类型 int,无论编译器是否真的坚持它。每个函数都应该有一个明确的返回类型。
【解决方案2】:

您的main() 函数启动:

main(
{

结束:

}
)

这是错误的。应该是:

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

void 是可选的。返回类型在现代 C 中不是可选的(C89/C90 标准允许它是可选的;C99 及更高版本要求它;即使您的编译器不坚持,您也应该像需要它一样进行编程)。正确的返回类型是 int(但请参阅 What should main return in C and C++? 了解完整详细信息)。

另外,作为Rizier123pointed outreturn0;main() 的末尾;那应该是return 0;

我还没有编译代码来查看还有哪些其他错误,但是括号和大括号的错误处理是最初错误的原因。

【讨论】:

  • 他还做了return0; 他必须在此处添加一个空格
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 2013-11-19
  • 2011-09-04
相关资源
最近更新 更多