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.
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?
CodePudding user response:
you need a forward declaration of call
void call(float integerrrr);
int main(){
float integerrrr=6;
call(integerrrr);
}
void call(float integerrrr){
printf("%f", integerrrr);
}
your compiler probably warned you about this
CodePudding user response:
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.
CodePudding user response:
the compiler of c work from top to bottom so line by line,
so u will have to call the first function void call()
then your main function:
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(){
float integerrrr=6;
call(integerrrr);
}