-1

I created a small program to convert temperature units from C° to F. the char unit needs to be reversed 'C' to 'F' and vice versa. in order to do so, I'm modifying the address of char unit directly in my function temp. now I'm a bit confused about how printf works when printing a function directly. precisely at this line:

printf("%.1lf %c", temp(input, &unit), unit);

my problem is that the printf is printing my unmodified unit even though my function already modified the value of char unit: result / expected

I can solve this by storing the function value into a double variable and printing it:

result = temp(input, &unit);
printf("%.1lf %c", result, unit);

could someone explain to me where my above logic is wrong
printf("%.1lf %c", temp(input, &unit), unit); It seems to me that printf is printing the value of my function first and then the unit. the unit value is being modified inside the function so I don't understand why it wasn't modified.

thanks a lot for your time.

#include <stdio.h>

double temp(int, char *);

int main(void) {
    int input = 0;
    char unit = 'a';
    double result = 0.0;
    printf("Temperature unit:");
    scanf("%d %c", &input, &unit);
    printf("%.1lf %c", temp(input, &unit), unit);
}

double temp(int temp, char * unit) {
    double output = 0.0;
    
    //convert to C°
    if (*unit == 'F') {
        output = (((double)temp - 32) * 5 / 9);
        *unit = 'C';
    }
    else if (*unit == 'C') {
        output = (double)temp * 9 / 5 + 32;
        *unit = 'F';
    }
    else {
        printf("wrong unit");
    }

    return output;
}
FatSumo
  • 79
  • 7

2 Answers2

0

printf("%.1lf %c", temp(input, &unit), unit); it is an undefined behaviour as the function called changes (side effect) the second parameter. In such a situation you cant do it this way.

you need to:

printf("%.1lf ", temp(input, &unit));
printf("%c", unit);

or

double temperature = temp(input, &unit);
printf("%.1lf %c", temperature, unit);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0___________
  • 60,014
  • 4
  • 34
  • 74
  • @FatSumo https://stackoverflow.blog/2011/01/08/how-to-say-thanks-in-an-answer/ – 0___________ Jun 28 '20 at 10:32
  • 2
    This is not undefined behavior. It is indeterminate. The function call `temp(input, &unit)` may be evaluated before or after the `unit` argument, but there will be a sequence point before the function call and after its `return`. So the evaluation of the `unit` argument and the change to it in `temp` are indeterminately sequenced but not unsequenced. – Eric Postpischil Jun 28 '20 at 11:18
  • I started programing about 3 months ago, would you have something I could read about concerning this behavior? thanks a lot – FatSumo Jun 28 '20 at 11:34
  • @FatSumo https://stackoverflow.com/questions/9566187/function-parameter-evaluation-order. Basically in C, within a construct that is a single statement, if some things do not have a *specific* cause-and-effect relation then they can be evaluated in any order, and the order can change for even the single statement from time to time. – Antti Haapala -- Слава Україні Jun 28 '20 at 11:42
  • If you want to be sure about the order of side effects, you need to split them into separate statements. – Antti Haapala -- Слава Україні Jun 28 '20 at 11:47
0

where my above logic is wrong (?)

Error is in "even though my function already modified the value of char unit".

The order of evaluation of arguments temp(input, &unit) and unit given to printf("%.1lf %c", temp(input, &unit), unit); is not defined.

unit may be evaluated before temp(input, &unit) or the other way around.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256