Why is the value of 0 being printed?

1

Hello, I have the following code in C

#include <stdio.h>

void main(void)
{
    int num, cociente, a, b, c, d;
    printf ("Evaluación de expresiones\n\n");
    a = 10; b = 4; c = 5; d = 1; num = 0;
    cociente = (num = a*b)/(num + c + d);
    printf("Cociente vale %d\n", cociente);
    printf("num vale %d\n", num);
    printf("\n\nTerminación normal del programa.\n");
}

I would like to know why the value of 0 is being printed in quotient, I know that the parentheses operator has higher priority and if there are two it is executed from left to right, then the first one is (num = a*b) if this parenthesis has priority I assume that must solve what is inside first, as there is an assignment and a multiplication, multiplication has higher priority and is solved first, then the assignment is made to num, being within the first parenthesis the variable num and with value of 40.

later the second parenthesis is solved, if num is 40 it is added with c + dy and a value of 46 is left in the second parenthesis, then the division between the two values is solved, 46/40, once solved is assigned the result to quotient.

Could you explain to me where my analysis error is, Thanks in advance

    
asked by EmiliOrtega 06.02.2017 в 23:51
source

3 answers

2

The mistake I just noticed, it's very turkey. (num = a*b) is worth less than (num + c + d) . Therefore, it will always give you 0.

    
answered by 07.02.2017 в 00:00
0

1st problem:

int cociente = (num = a*b)/(num + c + d);;
                ^^^^^^^^^

Would you say that num = a*b runs before or after num + c + d ?

  • If it is executed before, then:

    num = 10 * 4 = 40
    cociente = 40/(40 + 5 + 1) = 0
    
  • If it is executed later, then:

    cociente = 40 / 0 + 4 + 5 = 40 / 9 = 4
    

NO You should under no circumstances modify and use a variable in the same instruction. The compiler has total freedom (respecting the priority of operators) to determine the order of execution of the operations that make up an instruction. How much is b worth?

int a=1;
int b = a++ + ++a * a++;

2nd problem:

int num = 0, a = 10, b = 4, c = 5, d = 1;
int cociente = (num)/(num + c + d); // elimino la multiplicación por claridad

Type int does not use decimals. Before a division the result will appear truncated (if the result is 0.9999 you will get 0). To avoid this problem you have to use floating point types, namely float or double :

float cociente = ((float)num)/(num + c + d);

Of course, in order to get the correct result from the console you will have to indicate to printf that the number is in floating point instead of an integer:

printf("Cociente vale %f\n", cociente);
                      ^^ <--- AQUI
    
answered by 08.02.2017 в 10:06
0

In the expression cociente = (num = a*b)/(num + c + d); , (num = a*b) is not sequenced before (num + c + d)

According to the standard of C, §6.5 ¶2, if a modification of an object (= memory region, in this case the memory region named by num ), scalar is not sequenced with respect to an access to the value from that same object, Undefined Behavior occurs (UB = Undefined Behavior ).

The complete rule is: if a secondary effect (modification of an object, access to a volatile object, modification of a file or call to function that performs any of the previous operations) on a scalar object is not sequenced with respect to Another secondary effect on that same object or value calculation using the value of that same object, the behavior is not defined.

When Undefined Behavior occurs, the standard does not prescribe any behavior. The compiler could ignore it, give an error, or even produce incorrect code .

    
answered by 08.02.2017 в 13:15