0

I am a newbie seeking to learn C++. I have a book which gives many examples and concepts to play with. One of the first examples of the book does not compile using Quincy 2005. Is this a fault of the code or the IDE?

The example is here:

#include <stdio.h>

void main (){



int date = 10, d2=15;
float temp = 45.0, t2=33.5;


printf("On Dec. %d, temperature was %f.\n", date, temp);
printf("On Jan. %d, temperature was %f.\n", d2, t2);


}

Unfortunately, the IDE states that ::main must return an integer. Any ideas?

The example looks more like C, is this simply out of date?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
user1299661
  • 183
  • 2
  • 3
  • 11

3 Answers3

5

That's a really poor book you're using. That is a C example (not a single C++ feature in there) and won't compile on modern C++ compilers that enforce correct coding practices.

The correct code would look like this:

#include <stdio.h>

int main (int argc, char **argv)
{
    int date = 10, d2=15;
    float temp = 45.0, t2=33.5;


    printf("On Dec. %d, temperature was %f.\n", date, temp);
    printf("On Jan. %d, temperature was %f.\n", d2, t2);

    return 0;
}

Notice:

  • return type of main has been changed from void to int
  • main takes two arguments: the number of command line parameters, and their c-string values
  • main now returns a exit code

Are you sure it's a C++ book?

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 2
    `main` can take no arguments and not have a return statement, and an implementation may have other forms of `main` so long as the return type is `int` – David Brown Jul 06 '12 at 03:52
  • @David Yes, I know. But if you're learning to write, you might as well learn to do it the "proper" way. – Mahmoud Al-Qudsi Jul 06 '12 at 04:13
3

There are only two ways the C++ standard specifies as maximally portable ways to declare main. All others are implementation-defined. The following is from the lastest draft, section 3.6.1p2:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { /* ... */ } and int main(int argc, char* argv[]) { /* ... */ }

jxh
  • 69,070
  • 8
  • 110
  • 193
1

You cannot have a main() function that is of type void.

Change it to

#include <stdio.h>

int main ()
{
    int date = 10, d2=15;
    float temp = 45.0, t2=33.5;


    printf("On Dec. %d, temperature was %f.\n", date, temp);
    printf("On Jan. %d, temperature was %f.\n", d2, t2);

    return 0; /* The compiler will assume this for main() if you don't specify it */
}

and it will work fine. The reason it doesn't allow void for your main function is because the main function needs to return success or failure to the operating system. Convention is that returning 0 means the program executed successfully and any other number is an error code.

You can optionally add the argc and argv parameters to your main function, which are used to get values passed to your program from the command line. For example if your program is called "myprogram" then if you executed it from the command line using the following command:

./myprogram -test

then argc, which is the length of the array argv, would be 2, and argv would be an array with the following values:

argv[0] = "./myprogram";
argv[1] = "-test";
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • So long as you're splitting your answer to required and optional, the actual `return` statement is optional, and if omitted (in the `main` and only in the `main`), zero will be returned by default. – Mahmoud Al-Qudsi Jul 06 '12 at 04:58