Why don't I need a prototype for sqrt???

Hello All.

I apologize if I sound a bit dim-witted. I'm just diving into C++ (and programming in general) over the last week. I am using C++ Primer Plus as a guide.

I have been messing around with headers and prototypes recently, and have surprisingly found that the following code works, even without including the <cmath> header or a "double sqrt(double);" statement. I am using Visual Studio as my compiler. Any ideas on why I don't need a prototype for this function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
	double number;
	double square;

	using namespace std;

	cout << "Hi. My name is Timmy.\n";
	cin.get();
	cout << "I have an amazing gift.\n";
	cin.get();
	cout << "I have memorized the square root of every possible number." <<endl;
	cin.get();
	cout << "You don't believe me? Try me. Type in any number here: ";
    cin >> number;
	square = sqrt(number);
	cout << "\nThe square root of "
		<< number
		<< " is "
		<< square
		<< ".\n";
	cin.get();
	cin.get();
	cout << "Believe me now?";
	cin.get();
}
Last edited on
Might be included in the standard namespace. I'm not sure if that's compiler specific - if I try to run your code through the compiler on the forum (click the little gear icon at the top right of the code block), it won't compile because it doesn't recognize sqrt.
closed account (3hM2Nwbp)
The only likely explanation is that the <iostream> header on your platform includes <cmath> internally. You can view the <iostream> header source code through Visual Studio to confirm for yourself.

* Just to be clear, the standard does not guarantee this is the case for all platforms -- so the code you posted above is not portable, even if it does seem to work.
Last edited on
When I run that through the C++ Shell tool, I get:

In function 'int main()': 18:22: error: 'sqrt' was not declared in this scope
Topic archived. No new replies allowed.