views:

226

answers:

6

Hi,

I was provided this simple C++ [I think] program to investigate the maximum size of int that can be stored:

#include <limits.h>
#include <iostream>

void main ( int argc , char * argv[])

{

  cout << "INT_MAX      " << INT_MAX   << endl ;
  cout << "INT_MAX +1 = " << INT_MAX + 1  << endl ;
  cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ;

  cout << "INT_MAX / INT_MAX        " << INT_MAX /INT_MAX << endl ;
  cout << "(INT_MAX +1) / INT_MAX   " << (INT_MAX +1) /INT_MAX << endl;
  cout << "(INT_MAX -1) / INT_MAX   " << (INT_MAX -1) /INT_MAX  <<endl;
  cout << "INT_MAX / (INT_MAX +1)   " << INT_MAX  /(INT_MAX+1)  <<endl;
  cout << "INT_MAX / (INT_MAX -1)   " << INT_MAX  /(INT_MAX -1)  <<endl;

}

I'm attempting to compile with:

gcc -o int_max int_max.cpp

But I get the following error:

int_max.cpp:4: error: '::main' must return 'int'
int_max.cpp: In function 'int main(int, char**)':
int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope
int_max.cpp:9: warning: integer overflow in expression
int_max.cpp:13: warning: integer overflow in expression
int_max.cpp:15: warning: integer overflow in expression

I tried adding a return 0 at the end of main but that didn't help. Any idea what I've done wrong?

P.S It's possible this is actually a C snippet but I seem to remember the lecturer saying it was C++.

Cheers

+5  A: 

You are compiling C++ code with gcc in a file with .c extension?

// Use new C++ header files instead of their .h version.
#include <climits>
#include <iostream>

// cout and endl are declared in the std namespace.
using namespace std;

int main (int argc, char * argv[])    
{
  cout << "INT_MAX      " << INT_MAX   << endl ;
  cout << "INT_MAX +1 = " << INT_MAX + 1  << endl ;
  cout << "INT_MAX -1 = " << INT_MAX - 1 << endl ;

  cout << "INT_MAX / INT_MAX        " << INT_MAX /INT_MAX << endl ;
  cout << "(INT_MAX +1) / INT_MAX   " << (INT_MAX +1) /INT_MAX << endl;
  cout << "(INT_MAX -1) / INT_MAX   " << (INT_MAX -1) /INT_MAX  <<endl;
  cout << "INT_MAX / (INT_MAX +1)   " << INT_MAX  /(INT_MAX+1)  <<endl;
  cout << "INT_MAX / (INT_MAX -1)   " << INT_MAX  /(INT_MAX -1)  <<endl;

  return 0;
}

and use g++ to compile.

Mehrdad Afshari
Sorry no, I had tried it as both .c and .cpp, copy/pasted the wrong example.I was also under the impression that gcc compiled c++ too?
Adam Taylor
@Adam: I've always encountered problems compiling C++ code with `gcc`, even when the file extension is `.cpp`. It fails linking C++ libs.
Mehrdad Afshari
Fair enough, g++ seemed to work, so thanks!
Adam Taylor
@Adam: `gcc` actually *does* compile C++ files if they have `.cpp` extension but it doesn't link to C++ standard libraries. I've successfully compiled a `.cpp` file (without linking) and linked the `.o` file with `g++`.
Mehrdad Afshari
+1  A: 

Change the line

void main ( int argc , char * argv[])

to

int main (int argc, char *argv[])
Jedidja
No need for the `return`.
Konrad Rudolph
In fact it's not necessary to explicitly return 0. Check this: http://zamanbakshifirst.blogspot.com/2006/11/c-c-main-should-return-void.html
Manuel
Updated answer based on comments.
Jedidja
A: 

The first error is because the function main() is required by the C++ Standard to return an integer. Change to:

 int main ( int argc , char * argv[])

The remainder are because the objects named are in the std namespace. Add a using directive afuer your #includes:

#include <limits.h>
#include <iostream>
using namespace std;

The warnings are becaiuse when you say something like:

INT_MAX + 1

you are trying to create a value that is bigger than the biggest possible integer. This may or may not do what you want.

anon
Yes I explicitly want to see the output.
Adam Taylor
+2  A: 

Change

void main ( int argc , char * argv[])

to

int main ( int argc , char * argv[])

add a using statement after the includes:

using namespace std;

and at the end of main function:

return EXIT_SUCCESS;
codaddict
No need for the `return`.
Konrad Rudolph
Return statement is optional in `main()`, complaint is due to the `void main()` declaration
MSalters
No need to explicitly return anything. Check: http://zamanbakshifirst.blogspot.com/2006/11/c-c-main-should-return-void.html
Manuel
@Manuel: No need to syntactically, but semantically, not returning anything says "I don't know what state my application was in when it stopped running", whereas `return 0;` says "My application ran successfully." What message do you want to send to your maintenance programmers?
Bill
@Bill: fall-through in main means "success". I would expect my maintenance programmers to be well acquainted with that convention.
Manuel
@Manuel: fall-through either means "success" or "I forgot to put something here"... how do you tell which just by looking at foreign code?
Bill
A: 

As Mehrdad has pointed out you are compiling compiling c++ code from a file with a .c extension.

Also you will need a "using namespace std;" in after your #include if you aren't going to explicitly specify it for cout.

ScaryAardvark
+3  A: 

This is a problem that I tend to refer to as error message blindness. Consider your first error:

int_max.cpp:4: error: '::main' must return 'int'

The error here is that main() must return int. You currently have main() declared as returning void.

For this error message:

int_max.cpp:8: error: 'cout' was not declared in this scope
int_max.cpp:8: error: 'endl' was not declared in this scope

All standard library functions and objects are contained within the std namespace. That is, you can either:

std::cout << "whatever" << std::endl;

or:

using namespace std;

...

cout << "whatever" << endl;

Finally:

int_max.cpp:9: warning: integer overflow in expression
int_max.cpp:13: warning: integer overflow in expression
int_max.cpp:15: warning: integer overflow in expression

You have deliberately overflowed integers in these expressions. If you take the maximum number an integer can hold and add one to it, what happens? The compiler is warning you that you've done that.

Kaz Dragon
Yes I'm deliberately overflowing to see *exactly* what happens. And yes it is error message blindness as I never code in C++. Thanks though.
Adam Taylor