Q. Which of the following is the correct output for the program given below?
Code:
#include <stdio.h>
int main ( )
{
float floatvalue = 8.25;
printf ("%d\n " , (int) floatvalue);
return 0;
}
β
Correct Answer: (D)
8
Explanation:
In the given C program:
#include <stdio.h>
int main ( )
{
float floatvalue = 8.25;
printf ("%d\n", (int) floatvalue);
return 0;
}
- floatvalue is initialized as 8.25 (a float type).
- (int) floatvalue typecasts 8.25 to an int, which removes the decimal part, resulting in 8.
- The %d format specifier is used in printf(), which expects an int. Since we are explicitly typecasting floatvalue to int, it prints 8.
Correct Output:
(D) 8 β