<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
🌐
W3Schools
w3schools.com › c › c_math.php
C Math Functions
For a complete reference of math functions, go to our C <math.h> Library Reference.
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.

🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › math_h.htm
C Library - <math.h>
The math.h header defines various mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. There is only one macro defined in this library − Following are the
🌐
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 ·
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › math.h
C Programming/math.h - Wikibooks, open books for an open world
February 20, 2006 - math.h is a header file in the standard library of the C programming language designed for basic mathematical operations. Most of the functions involve the use of floating point numbers.
🌐
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.
🌐
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:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-library-math_h
C Library - math.h - GeeksforGeeks
July 23, 2025 - The math.h library in C provides a set of functions for performing mathematical operations.
🌐
Wikipedia
en.wikipedia.org › wiki › C_mathematical_functions
C mathematical functions - Wikipedia
February 14, 2026 - Some C libraries implement rand using arc4random_uniform internally. Beginning in C++17, C++ introduces special functions into the <cmath> header. Under POSIX systems like Linux and BSD, the mathematical functions (as declared in <math.h>) are bundled separately in the mathematical library libm.
🌐
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
🌐
Programiz
programiz.com › c-programming › library-function › math.h
C math.h
The C <math.h> header file declares a set of functions to perform mathematical operations such as: sqrt() to calculate the square root, log() to find natural logarithm of a number etc.
🌐
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.
🌐
cppreference.com
en.cppreference.com › w › c › header › math.html
Standard library header <math.h>
February 3, 2025 - C · [edit] Standard Library headers · [edit] This header is part of the mathematics library. #define __STDC_VERSION_MATH_H__ 202311L // TODO: ... Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/header/math&oldid=180107" Category: Todo with reason ·
🌐
Llvm
libc.llvm.org › math
math.h - LLVM C Library
LLVM-libc is actively used in ... standard library (regex, locale, wide-character I/O, etc.) will not yet compile against it: Static-linked Linux servers and containers — used in production at Google (servers and Pixel Buds) on x86-64 and AArch64. GPU compute (AMDGPU, NVPTX) — libc functions available in GPU kernels via LLVM’s offloading runtime. GPU docs · Baremetal embedded (ARM, RISC-V, AArch64) — minimal footprint builds for microcontrollers and custom hardware...
🌐
MIT
web.mit.edu › 10.001 › Web › Course_Notes › c_Notes › tips_math_library.html
Using the flag -lm (add 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.
🌐
Reddit
reddit.com › r/cprogramming › vs
r/cprogramming on Reddit: <math.h> vs <stdlib.h>
September 1, 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.
Top answer
1 of 14
324

The functions in stdlib.h and stdio.h have implementations in libc.so (or libc.a for static linking), which is linked into your executable by default (as if -lc were specified). GCC can be instructed to avoid this automatic link with the -nostdlib or -nodefaultlibs options.

The math functions in math.h have implementations in libm.so (or libm.a for static linking), and libm is not linked in by default. There are historical reasons for this libm/libc split, none of them very convincing.

Interestingly, the C++ runtime libstdc++ requires libm, so if you compile a C++ program with GCC (g++), you will automatically get libm linked in.

2 of 14
104

Remember that C is an old language and that FPUs are a relatively recent phenomenon. I first saw C on 8-bit processors where it was a lot of work to do even 32-bit integer arithmetic. Many of these implementations didn't even have a floating point math library available!

Even on the first 68000 machines (Mac, Atari ST, Amiga), floating point coprocessors were often expensive add-ons.

To do all that floating point math, you needed a pretty sizable library. And the math was going to be slow. So you rarely used floats. You tried to do everything with integers or scaled integers. When you had to include math.h, you gritted your teeth. Often, you'd write your own approximations and lookup tables to avoid it.

Trade-offs existed for a long time. Sometimes there were competing math packages called "fastmath" or such. What's the best solution for math? Really accurate but slow stuff? Inaccurate but fast? Big tables for trig functions? It wasn't until coprocessors were guaranteed to be in the computer that most implementations became obvious. I imagine that there's some programmer out there somewhere right now, working on an embedded chip, trying to decide whether to bring in the math library to handle some math problem.

That's why math wasn't standard. Many or maybe most programs didn't use a single float. If FPUs had always been around and floats and doubles were always cheap to operate on, no doubt there would have been a "stdmath".

🌐
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...