I want to display a value to a precision of 2 decimals in our project. So I decided to use float as data type for my variable - the basic knowledge which we all obtained when learnt C language.
However, to my surprise after compilation when I executed the binary, I see no print in the place of value of the float variable.
Below is the code snippet used -
float rate_sent = 0, rate_received = 0;
printf("\n Direct printf - %.2f/%.2f", rate_sent, rate_received);
Expected output -
Direct printf - 0.00/0.00
Actual output -
Direct printf - /
I tried using double as my data type. But no difference. I tried using %g as the format specifier, still no difference.
When I use the code snippet on any online C compilers it works fine, but when I am trying this code snippet in our code base, we don't see it work as expected. It looks to be basic C stuff but still not able to understand why it is not printing.
The software is simulated to work on x86_64 Linux environment. I am running my application on x86_64 RHEL VM. So I think the machine would support the floating point.
Could this be a limitation with the compiler?
Can you someone identify what could be the problem here?
Edit: However, when I use the %u format specifier, I see some garbage value
Figured out that float/double variables are declared and allocated space by the compiler. When the same variables are printing under gdb session value gets printed in decimal format. So it is just the printing issue. I think the printf function is overridden using macro to not process %f format specifier.