A

Akash Lawaniya • 9.58K Points
Tutor III

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;
}
  • (A) 0
  • (B) 0.0
  • (C) 8.0
  • (D) 8

Explanation by: Mr. Dubey

In the given C program:

#include <stdio.h>
int main ( )
{
      float floatvalue = 8.25;
      printf ("%d\n", (int) floatvalue);
      return 0;
}
  1. floatvalue is initialized as 8.25 (a float type).
  2. (int) floatvalue typecasts 8.25 to an int, which removes the decimal part, resulting in 8.
  3. 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

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics