πŸ“Š C Programming
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
πŸ’¬ Discuss
βœ… 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;
}
  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 βœ…

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 βœ…

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
276
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Akash Lawaniya
Publisher
πŸ“ˆ
96%
Success Rate