basic calculator question

The if statement does not work even when conditions are met for the code below. The output of float variable sum is always 0, what is the reasoning behind this. Furthermore if "sum = num1 + num2;" within the if statement is replaced with "printf("%f", num1+num2);" the output becomes very odd and random, for it even effects the float variable sum even when there is no reference to it is within the code.

note: using mingw compiler

%%Code

#include<stdio.h>
#include<conio.h>

int main(){
char operation;
float num1;
float num2;
float sum;

scanf("%c", &operation);

scanf("%f", &num1);
scanf("%f", &num1);

if (operation == 'A'|| operation == 'a'){
sum = num1 + num2;
}
printf("%f", &sum);
}

%%

%%OUTPUT

0.0000

%%
You are printing the floating point representation of the reference of your sum. Try replacing your printf statement with this:
1
2
3
printf ("%f", sum);
/* There is no '&' because you are passing the sum's value,
not a pointer to the sum. Easy to confused with, I know! */
And also

1
2
scanf("%f", &num1);
scanf("%f", &num1); // <-- do you mean num2 ?  
Topic archived. No new replies allowed.