Click here to Skip to main content
15,888,062 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C
#include <stdio.h>

int main()
{
printf("%f", 25/2);
return 0;
}


Why is this thing returning value 0.0000000

What I have tried:

What I have tried:

Tried changing %f to %d and it worked but why it donot work with %f?
Posted
Updated 24-Sep-21 4:10am
v2

Because the %f format specifier requires a double value, and you are providing an integer.
Try this:
C++
printf("%f\n", (double) 25/2);
Or this:
C++
printf("%f\n", 25.0/2);
 
Share this answer
 
Whenever there is a mismatch between the printf format specifier and its argument (as in your case) the function behaviour is 'undefined', hence you cannot blame your compiler.

You should compile with all warnings enabled (for instance with the -Wall switch for the gcc) in order to make the compiler warn you about such mismatches.
 
Share this answer
 
The previous solutions are all correct soI will add only this : as a beginner I recommend that you avoid compound operations as you are getting started. By that, I mean printing the result of an inline calculation is a compound operation which you found confusing. If you avoid this and try to write simpler code it will be less confusing for you. For example, this is another way to write what you have :
C++
#include <stdio.h>

int main()
{
    int result;
    result = 25 / 2;
    printf("%f\n", result );
    return 0;
}
That will also give you an incorrect output but you can see the intermediate result in the process. To get a better answer you should do something like this :
C++
int main()
{
    double result;
    result = 25.0 / 2.0;
    printf( "%f\n", result );
    return 0;
}
The key here is the format specification you pass to printf needs to agree with the arguments you pass to it as Mr. Pallini mentioned.
 
Share this answer
 
Early in my programming days I was teaching myself assembly language after learning FORTRAN. I learned a very important thing:

Often the compiler will let you get away with mismatched types - but that is asking for trouble. Sometimes, the part of the data read will not give the type of answer you expect (as in your problem). Other times, values may be truncated internally and give you incorrect calculations. It's also a bit language dependent so that's another problem with taking the (apparently) easy way out.

Put a little extra effort to getting your types matched from beginning-to-end in your calculations to avoid bugs that will surely drive you insane.
 
Share this answer
 
This is C, you are in charge of everything, you have to check everything.
C++
printf("%f", 25/2);

%f says that you expect a float, but 25/2 is an integer.
you need to say that you want the integer casted to float.
as stated in other solutions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900