Please ,explain this code. | SoloLearn: Learn to code for FREE!

+12

Please ,explain this code.

#include <stdio.h> #define PRODUCT(x)(x*x) int main() { int i=3,j,k,l; j=PRODUCT(i+1); k=PRODUCT(i++); l=PRODUCT(++i); printf("%d%d%d%d\n",i,j,k,l); return 0; } //Output:771249 I can't understand this output Please anyone explain it.

26 Answers

New Answer

+8

Macros are not functions. Macros work by substituting text during the preprocessor pass before the code reaches the compiler. It is possible to view the preprocessor output using command line switches: gcc uses -E msvc uses /E the output from the preprocessor to the compiler looks like this... int main(void) { int i=3,j,k,l; j=(i+1*i+1); k=(i++*i++); l=(++i*++i); printf("%d%d%d%d\n",i,j,k,l); return 0; } as you can see there are serious problems here because we have multiple auto increment statements occurring on the same line as well as the unexpected results due to the lack of parenthesis. The results are thus unpredictable and may vary from compiler to compiler. This is also why you should always surround macro arguments used in expressions with parenthesis. #include <stdio.h> #define PRODUCT(x)((x)*(x)) // <-- NOTE PARENTHESIS int main(void) { int i=3,j,k,l; j=PRODUCT(i+1); k=PRODUCT(i++); l=PRODUCT(++i); printf("%d %d %d %d\n",i,j,k,l); return 0; } results in... int main(void) { int i=3,j,k,l; j=((i+1)*(i+1)); k=((i++)*(i++)); l=((++i)*(++i)); printf("%d %d %d %d\n",i,j,k,l); return 0; } although there is still the problem of multiple auto increment statements on the same line.

+6

Thanks you ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ 🙏🙏🙏

+6

+4

variable I takes the values 3 4 5 with the result that you get output concatenated in the manner shown 🙄

+3

j = PRODUCT(i+1); will be: j = i + 1 * i + 1; Since variable i is equal to 3 and multiplication has higher oder or precedence j = i + ( 1 * i ) + 1; j = 3 + (1 * 3 ) + 1; That why j = 7 and at that moment variable i is still equal to 3. Variable i will turn into 7 after k and l are assigned since both statements will have 2 increment operators each. Do a step by step analysis. For the others. Its hard writing it in phone. Im a PC keyboard guy. Im at bed now.

+3

k = 3 * 4 = 12, its printing 12 and variable l is not printing just i, j, k, theres only 3 %d placeholders.

+3

+3

Sukriya for clear my doute 🗡️Jasprit and ✨Shivani✨ 😍😍

+2

Jasprit , okey so you think I am wrong..... But your answer , not give any satisfaction to me. So I am just trying to explain only... understand

+2

Sümît Kåßhyåp why dont you test the code and verify if your analysis is correct?

+2

Arturop yes I have tested my answer..... and I find it is correct...

+2

Okey brø , I am just trying to explain only ... brø. I didn't say that Jasprit is wrong....

+2

help me I take care that when the blue box touches the yellow the yellow disappears please https://code.sololearn.com/Wlg44q4f2JK6/?ref=app

+1

✨Shivani✨ Please give a suitable title to your question

+1

I want to goin to you tube premium, but my country forbidden

+1

Yah I don't know alot in this field, iam really sorry

+1

abhishek singh hes probably now just messing with us.

+1

Summit kashyap bro Send me ur code

+1

#include <stdio.h> #define PRODUCT(x)(x*x) int main() { int i=3,j,k,l; j=PRODUCT(i+1); k=PRODUCT(i++); l=PRODUCT(++i); printf("%d%d%d%d\n",i,j,k,l); return 0; }

+1

Then what happened bro?? Already explain by 🗡️Jasprit See that explanation brotehr