-4
#include<stdio.h>
int main(){
    int a=10,b=3,c=2,d=4,result;
    result = a+a*-b/c%d+c*d;
    printf("%d",result);
}

How is this program giving 15 as the output.. I did not understand the logic behind the operation.. Can someone please tell me how calculation is done?

  • 2
    What result did you expect and why? – HolyBlackCat Aug 15 '21 at 16:56
  • 2
    If you have trouble understanding a complex expression, split it into smaller and simpler parts, storing results in temporary variables. Continue until the small expressions can't be smaller (i.e. they only do a single operation). Then use a *debugger* to step through the code statement by statement, while monitoring variables and their values. Then you can easily see when a result becomes something it isn't supposed to be. – Some programmer dude Aug 15 '21 at 16:58
  • Use braces `()` when you are confused about the evaluation of the expression – Abhilekh Gautam Aug 15 '21 at 16:59
  • 1
    @AbhilekhGautam: The term "braces" refers to `{}`, not `()`, as those are called "parentheses". The general term for both is ["brackets"](https://en.wikipedia.org/wiki/Bracket). – Andreas Wenzel Aug 15 '21 at 17:02
  • As an addendum to my comment, it helps if you also know [the operator precedence](https://en.cppreference.com/w/c/language/operator_precedence) of the operators used. Which for mathematical operators happens to be the exact same as in real math (i.e. multiplication and division happens before addition or subtraction). – Some programmer dude Aug 15 '21 at 17:03
  • 2
    Your instructor gave you this exercise so that you would *think* and *learn* about C. If we just tell you what the expression does, you'll learn nothing. – Steve Summit Aug 15 '21 at 17:09

3 Answers3

4
int a = 10, b = 3, c = 2, d = 4, result;
result=a+a*-b/c%d+c*d;                            // original line, with no spaces added

result = a + (a * (-b) / c % d) + (c * d);
result = 10 + (10 * -3 / 2 % 4) + (2 * 4);
result = 10 + (-30 / 2 % 4) + 8;
result = 10 + (-15 % 4) + 8;
result = 10 + (-3) + 8;
result = 15;
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Isn't the behavior of modulo for negative values implementation-defined, meaning it's possible that the output would not be 15 in some cases? – mediocrevegetable1 Aug 15 '21 at 17:09
  • 3
    @mediocrevegetable1 Before C99, it was implementation-defined. But now it's well-defined. – Steve Summit Aug 15 '21 at 17:10
  • 1
    @mediocrevegetable1 See https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers – Andrew Henle Aug 15 '21 at 17:11
  • 1
    [C11 6.5.5](https://port70.net/~nsz/c/c11/n1570.html#6.5.5) defines the `%` operator objectively. I believe it was implementation defined before C99. – pmg Aug 15 '21 at 17:12
  • 1
    @mediocrevegetable1 The `%` is the **remainder** operator, not a *modulo* operator. – Adrian Mole Aug 15 '21 at 17:12
  • @AdrianMole I thought there wasn't a difference between the two, just checked and there is... :p – mediocrevegetable1 Aug 15 '21 at 17:15
  • 1
    @mediocrevegetable1 Indeed! If you start with a negative amount of apples, no matter what you divide by, you won't have a positive remainder. :-) – Adrian Mole Aug 15 '21 at 17:21
2

Note that the *, / and % operators have higher precedence than + and that those first three operators have equal precedence and left-to-right associativity. Note also that the unary minus operator (as in -b) has higher precedence than multiplication.

So, adding parentheses to highlight the operators' bindings and order-of-evaluation, and a couple of lines to keep track of the intermediate results, we see the following:

#include<stdio.h>
int main(){
    int a=10, b=3, c=2, d=4, result;
    result = a + ( ( ( (a*(-b)) ) / c ) % d ) + (c*d);
    //                   ^-30     ^-15   ^-3     ^ 8
    //         ^ a + -3 = 7                   ^ 7 + 8 = 15
    printf("%d",result);
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

The relevant code:

int a=10,b=3,c=2,d=4,result;
result=a+a*-b/c%d+c*d;

Now processing this as a compiler would, following the rules of operator precedence:

// Unary minus
result = 10 + 10 * (-3) / 2 % 4 + 2 * 4;

// Multiplication, division, and remainder, with left-to-right associativity.
result = 10 + (-30) / 2 % 4 + 8;
result = 10 + (-15) % 4 + 8; 
result = 10 + (-3) + 8;

// Addition and subtraction, with left-to-right associativity.
result = 15;

Note that according to the C99 specification, a == (a / b) * b + a % b must hold for the remainder operation with a negative number.

Yun
  • 3,056
  • 6
  • 9
  • 28