Value Assignment Doubt in c

Asked

Viewed 183 times

0

Here’s my code, why is there a change in the other variables, I’m not just changing the line line to variable value? why the others change?

int a = 1, b = 2, c = 3, valor = 0;

valor = a;
printf("%d\n" , valor);

valor = b++;
printf("%d\n", valor);

valor = ++c;
printf("%d\n", valor);

valor += b;
printf("%d\n", valor);

valor *= c;
printf("%d\n", valor);

2 answers

4

  • Case 1:

    valor = a; assigning a to the variable valor;

  • Case 2:

    valor = b++; is the same as the following sequence:

    valor = b;

    b = b + 1;

  • Case 3:

    valor = ++c; is the same as the following sequence:

    c = c + 1;

    valor = c;

  • Case 4:

    valor += b; is the same as the following sequence:

    valor = valor + b;

  • Case 5:

    valor *= c; is the same as the following sequence:

    valor = valor * c;

1


By doing b++ or ++b you are moving the variable b

It is different than doing value+=b, in this case only moves the variable "value"

  • then the statement for example: value=++b , is adding +1 in variable b in addition to changing a b , that’s it?

  • @Vitorgonçalves yes, this is increment operator, so it will add first to the variable b

  • summarizing: increment and decrease change the variables , no longer assignment?

  • @Vitorgonçalves exactly, when doing value = b++; //here the "b" would have the value of 2 value = b; // here the "b" would have the value of 3 value would have the value of 3, because when doing b++ then increments next

  • is a bit confusing, especially when ++ is in front of the kk variable, it 'calculates' but does not 'apply' the same as ++ ago , it is complicated but an hour will , thank you!! :)

  • 1

    x++ calculates but does not apply soon; ++x calculates and applies. Think this way for ease

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.