<math.h> is a header specified in the C standard. Its usage is supported in C++, but formally deprecated (which means, approximately, slated for potential removal from a future standard) by all C++ standards. I would suggest it is unlikely to be removed from a future C++ standard, for as long as backward compatibility to C is considered important or desirable.

<cmath> is a header specified in the C++ standard. It provides essentially the same functionality as in C's <math.h>, except that names (other than a couple of macros) reside in namespace std.

A similar story goes for <stdio.h> (C) and <cstdio> (C++), except that usage of stream I/O (e.g. <iostream>) is encouraged in C++.

Standard C++ headers never have a .hpp extension. That naming convention for headers is a convention encouraged by some, but is not formally required.

Answer from Peter on Stack Overflow
Top answer
1 of 4
12

<math.h> is a header specified in the C standard. Its usage is supported in C++, but formally deprecated (which means, approximately, slated for potential removal from a future standard) by all C++ standards. I would suggest it is unlikely to be removed from a future C++ standard, for as long as backward compatibility to C is considered important or desirable.

<cmath> is a header specified in the C++ standard. It provides essentially the same functionality as in C's <math.h>, except that names (other than a couple of macros) reside in namespace std.

A similar story goes for <stdio.h> (C) and <cstdio> (C++), except that usage of stream I/O (e.g. <iostream>) is encouraged in C++.

Standard C++ headers never have a .hpp extension. That naming convention for headers is a convention encouraged by some, but is not formally required.

2 of 4
2

The C++11 Standard says:

D.5 C standard library headers

1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, ...

The inclusion of these headers is stated as deprecated, meaning:

Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.

So they are still (just) part of C++.

They are provided for compatibility which is to allow the programmer to compile programs originally written for C with a standard conforming C++ compiler with little or no modification. That means things like not having to change the #include statements from <stdio.h> to <ctsdio>.

So the example given in cplusplus.com is actually standards conforming C++ that just happens to be compatible with a C90 and a C99 conforming C compiler. Presumably they do this because the page describing the math library gives information for both C and C++ languages following the standards for C90, C99, C++98 and C++11.

So to answer the specific questions:

1) Why are they using

<stdio.h>

I thought this was for C and not really for C++ ?

It's for C++ compatibility with C. Presumably they use it so the code will also compile on a C90/C99 conforming C compiler for which the page gives specifications.

1) Why are they using

<math.h>

I though the .h represented C header files rather than the .hpp C++ header files?

No. The standard does not specify what extensions files should use. In practice many C++ projects use .h as an extension for their header files.

I feel like I wont be able to explain myself if the teacher asks "why did you use a C header file?

Given that the C compatibility headers are deprecated (though probably not going anywhere) I would suggest it better to use the <cstdio> and <cmath> versions. However the idea that you are somehow writing C code simply because of your choice of library function is wrong. If it is legal C++ code being fed through a C++ compiler then it is C++. It may be more procedural in character and less object oriented in philosophy but it is nevertheless fully C++. Many, many, many C++ programs use libraries written in other languages, especially C. That doesn't make those programs somehow C.

🌐
Freshersnow
tutorials.freshersnow.com › home › c language tutorial › c math.h library function
C math.h Library Function - List of math.h Functions
November 5, 2019 - The C library function double ceil(double x) returns the smallest integer value greater than or equal to x. ... #include <stdio.h> #include <math.h> int main () { float val1, val2, val3, val4; val1 = 1.6; val2 = 1.2; val3 = -2.8; val4 = -2.3; printf ("value1 = %.1lf\n", ceil(val1)); printf ("value2 = %.1lf\n", ceil(val2)); printf ("value3 = %.1lf\n", ceil(val3)); printf ("value4 = %.1lf\n", ceil(val4)); return(0); }
🌐
W3Schools
w3schools.com › c › c_math.php
C Math
For a complete reference of math functions, go to our C <math.h> Library Reference.
🌐
Code-reference
code-reference.com › c › math.h
c math.h Programming | Library | Reference - Code-Reference.com
Mathematical functions Library Description acos computes arc cosine asin computes arc sine atan computes arc tangent atan2 computes arc tangent, using signs to determine quadrants ceil returns the nearest integer not less than the given value cos computes cosine cosh computes hyperbolic cosine ...
🌐
Quora
quora.com › When-do-we-use-math-h-in-C
When do we use math.h in C? - Quora
Answer (1 of 17): First of all allow me to correct you, there is no such thing as #math.h in C programming. you have a header file #include the header files are included in a program so that we can access the operations or functions that are available under this header. There are many h...
🌐
Cplusplus
cplusplus.com › reference › cmath
<cmath> (math.h)
C++11 math_errhandling · NAN · C++11 double_t · float_t · Reference · <cmath> header · C numerics library · Header <cmath> declares a set of functions to compute common mathematical operations and transformations: cos · Compute cosine (function) sin ·
Find elsewhere
🌐
Scaler
scaler.com › home › topics › math.h functions in c library
Math.h Functions in C Library| Scaler Topics
March 16, 2022 - In an attempt to minimize the language size, top-shelf operations are defined at the core — relational operators, arithmetic operators, and logical operators —while various extensions can be performed with the C standard library. The table summarizes the set of important math functions defined in math.h header file.
🌐
Medium
medium.com › @larmalade › gcc-the-hard-way-how-to-include-functions-from-the-math-library-1cfe60f24a7a
gcc The Hard Way: How to Include Functions from the Math Library | by Larry Madeo | Medium
February 12, 2017 - Even today, in the age of hand-held supercomputers, we add the -lmflag to the gcc command in order to include<math.h> which is necessary to use the floating point math functions available in the standard C library.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › index.php
C Language: Standard Library Functions - math.h
In the C Programming Language, the Standard Library Functions are divided into several header files. The following is a list of functions found within the <math.h> header file:
🌐
Reddit
reddit.com › r/cprogramming › vs
r/cprogramming on Reddit: <math.h> vs <stdlib.h>
October 12, 2020 -

we have just started learning about programming in c and our teacher told us that we must include the math library in order to perform functions like sqrt and pow. however im using code blocks for programming and it turns out that i didnt need to use that. is it because the stdlib.h has already been included?

Top answer
1 of 4
15
Possibly. The C standard specifies the standard C header files and the functions each of them declares. For instance, it says that the sqrt function is declared as: double sqrt(double x); when is included. The C standard does not say that the function is not declared when that header is not included. That is, it's entirely possible for the sqrt function to be declared through some other means, without you explicitly including . But in doing so you're relying on something that's outside of the C standard. Can you count on "that other something" having consistent, well-defined behaviour? No, not unless you can point to some kind of implementation-specific documentation that says it will always work. (If somebody thinks "but does that mean I can't write my own sqrt function, even if I make sure I don't include , because it might just 'accidentally' conflict with its C library declaration through some other means?"... then yes, you'd be right, you can't do that. The C standard actually calls this out in §7.1.3. The identifiers with external linkage defined by the C standard — all of the names of the standard C library functions, for instance — are always reserved for use as external-linkage identifiers by the C implementation, whether or not the standard header files declaring them are included.)
2 of 4
3
Are you sure you didn't need to include it? The C compiler usually accepts calls to functions that it does not know about. Then, it makes assumptions about what the types of the arguments and return values are. It can't check the type of values you pass in, it can't do any implicit conversions, it can't even check you passed the right number of values. Even it it builds and runs, it might be returning junk values.
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
#include <math.h>
June 26, 2010 - Hello, I am new to Arduino and electrical circuts as a whole but I am trying to get a thermistor to work with the Arduino and I have found some codes, but I cannot figure out how to include the #include import library but the math.h file is not one of the available files.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math.html
Common mathematical functions - cppreference.com
February 3, 2025 - 7.8 Format conversion of integer types <inttypes.h> (p: TBD) 7.12 Mathematics <math.h> (p: TBD) 7.22 General utilities <stdlib.h> (p: TBD) 7.31.5 Format conversion of integer types <inttypes.h> (p: TBD) 7.31.12 General utilities <stdlib.h> (p: TBD) C17 standard (ISO/IEC 9899:2018): 7.8 Format conversion of integer types <inttypes.h> (p: 158-160) 7.12 Mathematics <math.h> (p: 169-190) 7.22 General utilities <stdlib.h> (p: 248-262) 7.31.5 Format conversion of integer types <inttypes.h> (p: 332) 7.31.12 General utilities <stdlib.h> (p: 333) C11 standard (ISO/IEC 9899:2011): 7.8 Format conversion
🌐
Scribd
es.scribd.com › document › 784872980 › MATH-H-LIBRARY
Overview of math.h Functions in C | PDF
MATH.H LIBRARY - Free download as Text File (.txt), PDF File (.pdf) or read online for free.
🌐
MIT
web.mit.edu › 10.001 › Web › Course_Notes › c_Notes › tips_math_library.html
Math Library
First, put the following line near the top of each C file that will use math functions: #include <math.h> This '#include' statement includes the proper declarations for the math library functions. Second, link with the math library when you are compiling your program.
🌐
Quora
quora.com › How-do-I-compile-a-C-program-that-uses-math-h
How to compile a C program that uses math.h - Quora
Answer: Some C compilers require the math library be explicitly linked to the program. Apparently, the GCC C compiler is one of these. * See Why do you have to link the math library in GCC? A GCC compilation has a link stage where the math library called [code ]libm.a[/code] is used. [code]gcc...
🌐
W3Schools
w3schools.com › c › c_ref_math.php
C math (math.h) Library Reference
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The <math.h> library has many functions that allow you to perform mathematical tasks on numbers.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › math-functions
Math Functions in C Programming (Full List With Examples)
1 week ago - Learn about math functions in C programming with easy-to-understand examples. Learn how to use each function with clear syntax and practical applications.