Search code examples
c++cfloating-pointintformat-specifiers

Why does my function print '0'?


Here is my code, I don't exactly know why does this happen:

#include <stdio.h>

void foo(float *);

int main()
{
    int i = 10, *p = &i;
    foo(&i);
}

void foo(float *p)
{
    printf("%f\n", *p);
}

OUTPUT :

0

Solution

  • You are calling function expecting float* with a pointer to int. In C++ it is an error and your code does not compile. In C you will most likely get a warning and undefined behaviour anyway.