What will be the output of the following program: void main() { float val=2.; printf("%.2",val); }

  1. Compile-Time error

  2. 2.00

  3. %.2

  4. 2.000000


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compile-Time error - This option is incorrect because there is no compile-time error in the given program.

Option B) 2.00 - This option is incorrect because the printf statement in the program does not specify the format specifier for the float value. As a result, the output will not be 2.00.

Option C) %.2 - This option is correct because the printf statement uses the format specifier "%.2", which means it will print the float value with 2 decimal places. However, since the program does not provide any argument for the format specifier, it will print the format specifier itself as the output.

Option D) 2.000000 - This option is incorrect because the printf statement does not specify the format specifier for the float value. Therefore, the output will not be 2.000000.

The correct answer is C. This option is correct because the program will output the format specifier "%.2" as the output, which is "%.2".

Find more quizzes: