-4

Why is the output of 49/square(7) 49 in C?

I have a macro defined as #define square(rasna) rasna*rasna. When I use this macro in the expression 49/square(7), the output is 49. However, I expected the output to be 1. Why is this happening?

Here is the code:

#include <stdio.h>
#define square(rasna) rasna*rasna

int main()
{
int rasna;
rasna = 49/square(7);
printf("%d", rasna);
return 0;
}

I would appreciate it if someone could help me understand why the output is 49.

I defined a macro as #define square(rasna) rasna*rasna. I expected the output of the expression 49/square(7) to be 1, because 49 divided by 49 is 1. However, the output is actually 49.

Here is a table summarizing what I tried, what I expected to happen, and what actually happened:

Defined Macro Expected Output 49/square(7) Actual Output
square(rasna) rasna*rasna 1 49
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Macros in C are essentially only text replacements. You are calculating `49/7*7` which is 49 – chtz Aug 25 '23 at 02:09
  • A correct macro implementation would be `#define square(rasna) ((rasna)*(rasna))` – Marco Bonelli Aug 25 '23 at 02:10
  • still if i do it on plane paper it is 1 please do tell me this silly thing – Mohit_Yogi Aug 25 '23 at 02:10
  • @Mohit_Yogi if you do `49/7*7` on paper and you get one you are doing it wrong. If you wrote `49/(7*7)` that'd be another story. – Marco Bonelli Aug 25 '23 at 02:12
  • 1
    In C, operator precedence and associativity are two attributes that wouldn't necessarily hold elsewhere. In C, / and * are equal in precedence and then their associativity is left to right, which means the compiler creates code that executes 49 / 7 and the result, which is 7, is multiplied by 7. That final result is 49. It's all in the documentation. Just look up "C operator precedence". – Jeff Holt Aug 25 '23 at 02:18
  • 2
    Duplicate of any or all of the following questions: (1) [The need for parentheses in macros in C?](https://stackoverflow.com/q/10820340/15168) — (2) [C macros and use of arguments in parentheses](https://stackoverflow.com/q/7186504/15168) — (3) [Can we remove parentheses around arguments in C macros?](https://stackoverflow.com/q/27752386/15168) — (4) [When can the parentheses around arguments in macros be omitted?](https://stackoverflow.com/q/20964369/15168) — and there may be others. – Jonathan Leffler Aug 25 '23 at 04:38
  • 1
    Note that with `#define square(x) x * x`, if you evaluated `49 / square(4 + 3)`, you would not get `1`; you'd get `49 / 4 + 3 * 4 + 3` which is `27`. Make sure you understand why! If you simply wrapped the macro body in parentheses — `#define square(x) (x * x)` you'd get `49 / (4 + 3 * 4 + 3)` which is `2`. To get `1` as intended, you must use `#define square(x) ((x) * (x))` so you'd get `49 /((4 + 3) * (4 + 3))` — now you'll get the result `1`. – Jonathan Leffler Aug 25 '23 at 04:58

1 Answers1

3

The preprocessor applies your macro and the code becomes:

int main()
{
    int rasna;
    rasna = 49/7*7;
    printf("%d", rasna);
    return 0;
}

So order of operations makes the line calculate like (49/7)*7, which is 49. Normally these macros are wrapped in parentheses to avoid this gotcha:

#define square(rasna) ((rasna)*(rasna))
theSparky
  • 440
  • 3
  • 13