0

I am confused why this code works without including

"#include <cmath>"

below is my code and it works without it.

// PowerApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int main()
{
    std::cout << pow(2, 3) << "\n";
    return 0;
}

I referred What is the C++ function to raise a number to a power? which again confirms that we need it.

I checked definition of pow and it is part of <cmath> but it works as soon as I include <iostream>

SunMan
  • 214
  • 1
  • 12

1 Answers1

1

To begin with, any C++ system header file ending with .h are from before C++ was standardized 20 years ago, they are obsolete and should never be used.

And header files might include other header files, but there's nothing in the standard that requires it. If you need math functions then include <cmath> explicitly.

If you wonder about what headers are needed for which functions and/or classes, I recommend a reference site like this one.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • **−1** Silly claim: "To begin with, any C++ system header file ending with .h are from before C++ was standardized 20 years ago, they are obsolete and should never be used.". Most C++ standard library .h headers use C++ features such as overloading, that were never part of C. The question does mention the pre-standard header `` header. But that context is not established for the claim. – Cheers and hth. - Alf Jul 23 '18 at 04:33
  • i didn't find cmath in iostream – SunMan Jul 23 '18 at 05:22
  • 1
    @SunMan As I said, if you need functionality from the `` header file, you must explicitly include it. – Some programmer dude Jul 23 '18 at 05:40
  • @Cheersandhth.-Alf the question **did** mention the pre-standard header header before it was edited. – Jabberwocky Jul 23 '18 at 06:10
  • @Jabberwocky: It seem you're trying to create a misleading impression of something. My comment does mention what the question was before it was edited. And that comment was made before the question was edited. And so your comment is meaningless to me except as a create-impression thing. – Cheers and hth. - Alf Jul 23 '18 at 06:12
  • 2
    @Cheersandhth.-Alf I don't really see your point. Neither the question nor my answer mentions C. The question (in its original state) indicated that the OP might want to use `` as it (on the OP's system) also included ``. I warn that `` (or rather *any* **C++** "standard" header file ending with `.h`) aren't standard at all and should not be used. – Some programmer dude Jul 23 '18 at 06:14
  • @SunMan In your implementation `iostream` includes `istream` which includes `ostream`, which includes `ios` which includes `xlocnum` which includes `cmath`. But these are implementation details. As already mentioned, if you need `cmath`, include it. – Jabberwocky Jul 23 '18 at 06:16
  • understood.. now i see why it worked. thanks @Jabberwocky – SunMan Jul 23 '18 at 20:18