What is the result of the expression abs(-7.5) in C?
A. -7.5
B. 7.5
C. 7
D. Error
Answer: Option D
Solution (By Examveda Team)
In C, the functionabs() 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
Comments (1)
Related Questions on Function
In C, what is a function primarily used for?
A. Decision making
B. Variable declaration
C. Code organization and reusability
D. Printing output
What is the purpose of a function prototype in C?
A. To declare a function's return type
B. To declare a function's name
C. To declare a function's parameters and return type
D. To declare a function's name and parameters
In C, how are function arguments passed by default?
A. By value
B. By reference
C. By pointer
D. By address

f you're seeing 7 as the result, that likely means your compiler is implicitly converting -7.5 to int (-7), then applying abs(), which results in 7.
But this behavior is not standard and should be avoided.