Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot print called float variables

Im new to C and coding in general so, sorry if this issue is super easy to solve but. I cannot print called float type variables into new functions. INT type variables i can print but float just does not want to print its value when called into a new function.

First i tried to change the floats to ints and that worked

int main(){
int integerrrr=6;
call(integerrrr);
}

void call(int integerrrr){
printf("%d", integerrrr);

}

This worked and it did indeed print 6.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

BUT doing the same but with floats provides 0.00000:

int main(){
float integerrrr=6;
call(integerrrr);
}

void call(float integerrrr){
printf("%f", integerrrr);

}

How do i correctly call and then print float variables?

>Solution :

You could simply define call above main instead of below it. The compiler must have seen the declaration of functions when they are used, so a forward declaration like pm100 suggests is one way. Moving the whole definition above main is the another (that does not require a forward declaration).

#include <stdio.h>

void call(float integerrrr){
    printf("%f", integerrrr);
}

int main(void){
    float integerrrr = 6;
    call(integerrrr);        // now the compiler knows about this function
}

INT type variables i can print but float just does not

If your program actually compiles as-is it will use an old (obsolete) rule that makes an implicit declaration of undeclared functions when they are used. The implicit declaration would be int call(); – which does not match your actual function. The program (even the one that seems to be working) therefore had undefined behavior.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading