+3

Why the ouput is 9 7:::

#define sqr(i) i*i int main (){ printf("%d %d",sqr(3),sqr(3+1)); }

c

3/27/2020 3:36:33 PM

Amine Laaboudi

3 Answers

New Answer

+3

sqr is a macro and it is textually replaced at the preprocessor stage so sqr (3) becomes 3 * 3 sqr(3+1) becomes 3+1*3+1 which is later evaluated to 9 and 7 respectively

+2

sqr(i) is defined as i*i by you so:- sqr(3+1) will be replaced with 3+1*3+1 which when calculated using BODMAS we get 3+3+1=7.

+1

sqr(3+1) is calculated as 3+1*3+1 In c, first preference is given to multiplication(*) and then comes addition(+) hence in 3+1*3+1 , 1*3 is first calculated and then all the three numbers are added. If you are expecting the result as 16, try sqr((3+1)) the above code is considered as (3+1)*(3+1) here, first the operations inside the brackets are performed followed by the multiplication.