Which format specifier is used to print a float with two decimal places?
A
%f B
%0.2f C
%.2f D
%2f
Analysis & Theory
`%.2f` limits the output to two digits after the decimal point.
What will be the output?
```c
float x = 3.4567;
printf("%.2f", x);
```
A
3.4567 B
3.45 C
3.46 D
3.5
Analysis & Theory
`%.2f` rounds `3.4567` to `3.46`.
Which data type gives more precision in decimal values?
A
float B
int C
char D
double
Analysis & Theory
`double` provides higher precision than `float` in C.
What is the default number of decimal places printed by `%f` in `printf()`?
A
2 B
4 C
6 D
8
Analysis & Theory
By default, `%f` prints six digits after the decimal point.
What is the output?
```c
printf("%.0f", 5.99);
```
A
5.99 B
5.0 C
5 D
6
Analysis & Theory
`%.0f` rounds to the nearest whole number, which is `6`.
Which of the following is a correct way to **round** a number to one decimal place?
A
printf("%.1f", 7.89); B
printf("%1f", 7.89); C
printf(".1f", 7.89); D
printf("%f.1", 7.89);
Analysis & Theory
`%.1f` correctly specifies 1 decimal place.
What will this print?
```c
double d = 2.123456;
printf("%.4f", d);
```
A
2.1234 B
2.1235 C
2.123456 D
2.12
Analysis & Theory
`%.4f` prints four digits after the decimal and rounds the last digit.
Which statement best describes the effect of using `%.2f` in `printf()`?
A
It changes the data type B
It truncates all digits after decimal C
It rounds the number to two decimal places D
It adds two zeros to all numbers
Analysis & Theory
`%.2f` rounds the value to exactly two decimal digits.
What is the output?
```c
float x = 7.0 / 3;
printf("%.3f", x);
```
A
2.333 B
2.3333 C
2.33 D
2.3
Analysis & Theory
`7.0 / 3` gives `2.333...`, and `%.3f` shows three digits after the decimal.
Which format specifier will print `3.14159` as `3.14`?
A
%.1f B
%.2f C
%.3f D
%.4f
Analysis & Theory
`%.2f` displays only two digits after the decimal, rounding where needed.