Which header should be added so that the following program using log(36.0) compiles and links correctly?
#include
int main()
{
printf("%f
", log(36.0));
return 0;
}
-
A#include<conio.h>
-
B#include<math.h>
-
C#include<stdlib.h>
-
D#include<dos.h>
Answer
Correct Answer: #include<math.h>
Explanation
Introduction:The function log is part of the C math library. This question checks whether you can identify the correct header needed to declare and use log properly.
Given Data / Assumptions:
- Function used: log(36.0)
- Output via printf with %f
- Standard C environment
Concept / Approach:In C, mathematical functions such as log, exp, sin, and cos are declared in math.h. Including the proper header ensures correct declarations and types. Some toolchains also require linking with the math library (e.g., -lm) at link time.
Step-by-Step Solution:1) Identify log as a natural logarithm function.2) Recall its declaration resides in math.h.3) Add the line #include
Verification / Alternative check:Compile with and without the header to see that the version without math.h yields warnings or errors about implicit declaration or mismatched types.
Why Other Options Are Wrong:conio.h: Non-standard header for console I/O on some DOS compilers; unrelated to log.stdlib.h: General utilities (malloc, exit, atoi, etc.), not log.dos.h: Platform-specific DOS functions; not math.
Common Pitfalls:Forgetting to link the math library on Unix-like systems. Also, using %f in printf with log is fine because log returns double.
Final Answer:#include