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.
compilation - Why do you have to link the math library in C? - Stack Overflow
is <math.h> for C or C++?
<math.h> vs <stdlib.h>
Is #include <math.h> necessary?
Videos
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.
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".
<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.
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.
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?
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?