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.

Answer from ephemient on Stack Overflow
🌐
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.
🌐
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
Discussions

compilation - Why do you have to link the math library in C? - Stack Overflow
If I include or in a C program, I don't have to link these when compiling, but I do have to link to , using -lm with GCC, for example: ... What is the reason for this? Why do I have to explicitly link the math library, but not the other libraries? More on stackoverflow.com
🌐 stackoverflow.com
is <math.h> for C or C++?
For C++ use rather than . ... A header by that name is available in both C and C++ standard libraries, and is usually the same file. More on stackoverflow.com
🌐 stackoverflow.com
<math.h> vs <stdlib.h>
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.) More on reddit.com
🌐 r/cprogramming
8
6
September 3, 2020
Is #include <math.h> necessary?
The compiler's are taking their best guess that you want the pow() function from math.h and are telling you to either properly include the header or define your own pow function. If your intent is to use the built in pow() function you need to include the header both for clarity and to ensure the code can be compiled with stricter warning and error settings. If you tried to compile with the -Werror flag it would fail. More on reddit.com
🌐 r/C_Programming
14
6
January 31, 2023
🌐
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...
🌐
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.
🌐
GitHub
gist.github.com › 4033545
math.h · GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/vertrigo/4033545.js&quot;&gt;&lt;/script&gt;
🌐
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 ·
Find elsewhere
🌐
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.
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".

🌐
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.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 9699919799 › basedefs › math.h.html
<math.h>
Applications should use FLT_MAX as described in the <float.h> header instead of the obsolescent MAXFLOAT. Note that if FLT_EVAL_METHOD is neither 0 nor 1, then some constants might not compare equal as expected; for example, (double)M_PI == M_PI can fail. Before the ISO/IEC 9899:1999 standard, the math library was defined only for the floating type double.
🌐
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.
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.

🌐
Reddit
reddit.com › r/cprogramming › vs
r/cprogramming on Reddit: <math.h> vs <stdlib.h>
September 3, 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.
🌐
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 ·
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-library-math-h-functions
C Library math.h Functions - GeeksforGeeks
April 3, 2023 - The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result.
🌐
GitHub
github.com › SolutionOP › Math
GitHub - SolutionOP/Math: Project on writing own Math.h library in C language
The functions of the math.h library must be implemented (only those directly described above): The library must be developed in C language of C11 standard using gcc compiler
Author   SolutionOP
🌐
GNU
ftp.gnu.org › old-gnu › Manuals › glibc-2.2.3 › html_chapter › libc_19.html
The GNU C Library - Mathematics
This chapter contains information about functions for performing mathematical computations, such as trigonometric functions. Most of these functions have prototypes declared in the header file `math.h'. The complex-valued functions are defined in `complex.h'.
🌐
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
🌐
Reddit
reddit.com › r/c_programming › is #include necessary?
r/C_Programming on Reddit: Is #include <math.h> necessary?
January 31, 2023 -

So I started to study C and noticed something interesting.

TL;DR:

Building the code gives me a warning/error, telling me to declare the function or to include math.h. But I can ignore that message and still run the code and the function gets executed. I build the code by using gcc code_name.c -o code_name or just "run code" with Vs Code's Code Runner. Both will give me the error but running the code with ./code_name works fine, the functions are applied. I can ignore the error message and still run the code. If you use Replit, it works as well, without any messages.

I use VS Code and run my code in two different ways. Either Code-Runner or the Terminal, usually Code-Runner and Terminal when it doesn't work with it.

When running the following code:

#include <stdio.h>
// #include <math.h>

int main(){
    printf("%f\n", pow(2,3));
    return 0;
}

This happens with Code-Runner:

>https://imgur.com/a/YsgSyvq (First Image)

Note that Code-Runner automatically creates the .exe and I can run the code with the terminal (PowerShell) by using

./code_name

>https://imgur.com/a/YsgSyvq (Second Image)

But again, when I try to use

gcc Working_Numbers.c -o WN     

to build the code, it sends a warning message, telling me that I must use math.h or declare pow() (the math function).

But I can still do

./WN

And the code runs without issues.

>https://imgur.com/a/YsgSyvq (Third Image)

So does C already have these math functions built into it or is #include <math.h> necessary?