2

I've been trying to learn C from a book called C Programming Absolute Beginner's Guide and I have come across a problem and the one bad thing is you can't ask a book a question! Typed the error into Google and it brought me to this site. The error I'm getting is in the question title and this is my code.

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

)

What's wrong with this code?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Welcome to Stack Overflow. Please read the [About] page soon. When you have a compilation error, it is best to include the exact error message — except that if the file name is a mile long absolute path, reduce the name to just the base name of the file (so change `/huge/long/path/with/many/levels/of/directory/src/file.o` to just `file.o`). Make sure the source you show and the line numbers in it match what the compiler says. If the file is too long for comfort, create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) – Jonathan Leffler Jan 04 '15 at 20:28
  • 1
    I think the last character `)` should be right after `main(` so -> `main()` Does that do the trick for you? (BTW: `return0;` -> `return 0;`) – Rizier123 Jan 04 '15 at 20:29
  • I looked at the sample material for the book, and (in chapter 2) the sample programs all have `main()` rather than `int main()` (or, as I'd prefer, `int main(void)`). They do include the `return 0;` that's necessary since they aren't using C99 notations which permit the `return 0;` to be omitted (though I don't like code that does that, either). The 3rd Edition is dated 2014; it is a bit worrying that the first programs haven't been written in current C for about 15 years now. I don't like its characterization of `return`, `while`, `int`, `if`, `float` as 'commands' — they're keywords! Beware! – Jonathan Leffler Jan 05 '15 at 01:12
  • @Dave: Please mark one answer as solution. And if you can: delete your "thank you answer" as this doesn't answer the question. – Simon Sobisch Nov 15 '16 at 10:32

2 Answers2

2

Your main function is not good.The compiler says it.

It should look like

main()
{
....
}

Instead of

main(
{
...
}
)
Gábor Csikós
  • 2,787
  • 7
  • 31
  • 57
  • 2
    In modern C (anything written since 2000), the `main()` function should have the explicit return type of `int`, regardless of whether the compiler actually insists on it or not. Every function should have an explicit return type. – Jonathan Leffler Jan 04 '15 at 20:36
1

Your main() function starts:

main(
{

and ends:

}
)

This is wrong. It should be:

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

The void is optional. The return type is not optional in modern C (the C89/C90 standard allowed it to be optional; C99 and later requires it; you should program as if it is required even if your compiler doesn't insist on it). The correct return type is int (but see What should main return in C and C++? for the full details).

Also, as Rizier123 pointed out, have return0; at the end of main(); that should be return 0;.

I've not compiled the code to see what other errors there are, but the mishandling of parentheses and braces was the cause of the initial error.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278