-3
#include <stdio.h>
#include <stdlib.h>

int main() {

    int a=100;
    
    printf("%f",a);

    return 0;

    }

As we can see I declared a variable a with integer data type, so that it's going to allocate 4 bytes of space.

Next in printf I have used %f ( float) that means it have to print in float value whatever the input is, right?

But when I execute the code i'm getting 0.000000 as an output (whatever value I give it's just giving the output 0.0000).

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    *that means it have to print in float value whatever the input is right?* **Wrong** it means the `"%f"` **must match** with a value of type `double` (or `float`). Matching `"%f"` with anything else is **wrong** and invokes **Undefined Behaviour**. *Note: a value of type `float` is automaticaly converted to type `double` even before `printf()` is called.* – pmg Nov 06 '21 at 11:01
  • Try `printf("%f\n", (double)a);` – pmg Nov 06 '21 at 11:05
  • ya its working , tq very much (actully i misunderstood that concept i thought "that means it have to print in float value whatever the input is right" this is correct tq – varun prasad Nov 06 '21 at 11:13
  • `a` is an integer and if you try to parse it like a float, that will happen. Nothing unexpected. Integers and floats are stored differently, by using %f, you're telling the system that `a` is stored as a float, which is untrue. – Shambhav Nov 06 '21 at 11:33
  • @ShambhavGautam got cha / concept clear tq – varun prasad Nov 06 '21 at 13:31

1 Answers1

-3

Try this:

printf("%f", (double)a);

The reason it did not work was wrong formatting syntax. You can change the whole syntax, or rather use the casting option above.

Note: you can include any int value in float range, but cannot do otherwise.

Mabadai
  • 347
  • 4
  • 16
  • ya tq when i tried that its working but , from what i have learned %f in printf is nothing but it is just telling the printf function to print in float na ,, even though the the value of a is an integer oor what ever the printf fuction doest know that na thats my doubt !! plz correct me if am wrong – varun prasad Nov 06 '21 at 11:03
  • @varunprasad _"from what i have learned %f in printf is nothing but it is just telling the printf function to print in float na even though the the value of a is an integer"_: what you have learned is either wrong or you've misunderstood something – Jabberwocky Nov 06 '21 at 11:04
  • 1
    ok , can plz say that what is the actuall menaing of %f (or any thing )weather it going to print in that format or its is expecting only in that format ? tq – varun prasad Nov 06 '21 at 11:09
  • 1
    Actually `"%f"` expects a matching argument of type `double` ... and any argument of type `float` used in a function accepting a variable number of arguments (such as `printf()`) is automatically converted to type `double`. You can avoid this double conversion with `printf("%f", (double)a);` – pmg Nov 06 '21 at 11:26
  • @pmg Good point, updated! – Mabadai Nov 06 '21 at 11:30