C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 5)
5.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    float a=3.15529;
    printf("%2.1f\n", a);
    return 0;
}
3.00
3.15
3.2
3
Answer: Option
Explanation:

float a=3.15529; The variable a is declared as an float data type and initialized to value 3.15529;

printf("%2.1f\n", a); The precision specifier tells .1f tells the printf function to place only one number after the .(dot).

Hence the output is 3.2

Discussion:
15 comments Page 2 of 2.

Samarth Goyal said:   7 years ago
int main()
{
float a=3.15529;
printf("%b.1f\n", a);
return 0;
}

Varying b simply indents the result. What really matters or rather what will affect the output is the digit after the decimal.

say b=3, in that case the output will be printed upto 3 decimal places.

Nishtha Khare said:   7 years ago
Main()
{
float a=0.7;
if(a<0.7)
printf("c");
else
printf("c++");
}

Why the output of this program will be "c"?

Raeesa said:   7 years ago
What will be the output of printf("%f",(float)9.5)?

Pradeep Singh said:   5 years ago
@All.

1. In %2.1f, 2 is the left padding (blank spaces).
2. It is printing 3.2 instead of 3.1, because it is rounding of the float value.

Palak said:   4 years ago
Wht if i dont want the decmal point to change from for eg : 3.09 to 3.1 when i am printing only one decimal point.


Post your comments here:

Your comments will be displayed after verification.