?
What is the result of the expression abs(-7.5) in C?
Answer & Solution
Correct Answer:
Option
D
In C, the function
Passing a floating-point number like
To handle floating-point numbers, C provides a different function called
Therefore, using
Option A and B are incorrect because
Option C is incorrect because while
Hence, the correct answer is Option D: Error.
abs() is defined in stdlib.h and is used to compute the absolute value of an integer.Passing a floating-point number like
-7.5 to abs() is incorrect because abs() expects an int argument.To handle floating-point numbers, C provides a different function called
fabs(), which is defined in math.h.Therefore, using
abs(-7.5) results in a compile-time error or unexpected behavior because the argument type does not match the function's expected parameter.Option A and B are incorrect because
abs() cannot legally process a float.Option C is incorrect because while
7 is the integer part, the input is still a float and would not be accepted by abs().Hence, the correct answer is Option D: Error.
Join the Discussion
Login to post a comment or share your explanation.
LoginBut this behavior is not standard and should be avoided.