C Programming - Input / Output - Discussion

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;
}

[A]. 3.00
[B]. 3.15
[C]. 3.2
[D]. 3

Answer: Option C

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


Sriram said: (Aug 5, 2011)  
What is the purpose of 2 in "2. 1f"?

Atul Sharma said: (Nov 24, 2011)  
It mean there are all 2 decimal values which is going to print and. 1 mean there is one value after decimal.

Dinesh said: (Oct 11, 2012)  
No it's wrong sharma. 2 means it takes 2 empty spaces before printing a value on the screen.

Ash said: (Nov 22, 2013)  
int main()
{
float a=3.15529;
printf("%3.1f\n", a);
return 0;
}

What is the answer for this?

Shekhar said: (Nov 27, 2013)  
3.2 is the answer.

3 means it will take 3 decimal values into consideration.

Shirshendu Deb Roy said: (Sep 2, 2014)  
printf("%9.2f",12.45);

This statement outputs four spaces and then 12.45. Those four spaces plus 12.45 (five characters total) equal the 9 in the width.

Only two values are shown to the right of the decimal because .2 is used in the %f conversion character.

#include <stdio.h>
int main()
{
float sample1 = 34.5;
float sample2 = 12.3456789;
printf("%%9.1f = %9.1f\n",sample1);
printf("%%8.1f = %8.1f\n",sample1);
printf("%%7.1f = %7.1f\n",sample1);
printf("%%6.1f = %6.1f\n",sample1);
printf("%%5.1f = %5.1f\n",sample1);
printf("%%4.1f = %4.1f\n",sample1);
printf("%%3.1f = %3.1f\n",sample1);
printf("%%2.1f = %2.1f\n",sample1);
printf("%%1.1f = %1.1f\n",sample1);
printf("%%9.1f = %9.1f\n",sample2);
printf("%%9.2f = %9.2f\n",sample2);
printf("%%9.3f = %9.3f\n",sample2);
printf("%%9.4f = %9.4f\n",sample2);
printf("%%9.5f = %9.5f\n",sample2);
printf("%%9.6f = %9.6f\n",sample2);
printf("%%9.7f = %9.7f\n",sample2);
printf("%%9.6f = %9.6f\n",sample2);
printf("%%9.7f = %9.7f\n",sample2);
printf("%%9.8f = %9.8f\n",sample2);
return(0);
}

Output:

%9.1f = 34.5
%8.1f = 34.5
%7.1f = 34.5
%6.1f = 34.5
%5.1f = 34.5
%4.1f = 34.5
%3.1f = 34.5
%2.1f = 34.5
%1.1f = 34.5
%9.1f = 12.3
%9.2f = 12.35
%9.3f = 12.346
%9.4f = 12.3457
%9.5f = 12.34568
%9.6f = 12.345679
%9.7f = 12.3456793
%9.8f = 12.34567928

Chandu said: (Feb 3, 2016)  
What is the solution for this?

#include<stdio.h>
int main()
{
float a=1244.5678;
scanf("%3.2f",&a);
printf("%f",a);
}

Please explain it clearly.

Mahmoud Abulaaty said: (Mar 9, 2017)  
@Chandu.

1244.567749 is the answer.

This is because you have already put a value into "a" and the max size of float is to give 1244.567749 not 1244.5678 as I think.

Anitha said: (Apr 10, 2017)  
int main()
{
float a=3.15529;
printf("%2.1f\n", a);
return 0;
}
2 here represents the 2positions to consider so we take 3.155, since it is 5 we round it as 3.2 and print the value is it.

Am I right?

Someone explain it if I'm wrong.

Samarth Goyal said: (Oct 7, 2017)  
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: (Nov 1, 2017)  
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: (Nov 16, 2017)  
What will be the output of printf("%f",(float)9.5)?

Suraj Kumar said: (Nov 11, 2018)  
@All.

Why the compiler is printing 3.2? instead it should have to print 3.1.

Because it should print one value after decimal that's why it is printing 2. Am I right?

Pradeep Singh said: (Jul 23, 2019)  
@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: (Aug 27, 2020)  
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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.