+ 1

Why the answer is -1?

#include <stdio.h> #define square(x) x*x int main() { int i=square(4-1); printf("%d",i); return 0; } https://code.sololearn.com/cwpxqN047TC7/?ref=app

5th Jan 2022, 8:10 PM
HamidReza
HamidReza - avatar
5 Respostas
+ 10
HamidReza , evaluate it => 4-1*4-1=> 4-4-1=> - 1.
5th Jan 2022, 8:13 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
thnx.
5th Jan 2022, 8:15 PM
HamidReza
HamidReza - avatar
+ 2
#include<stdio.h> #define square(x) x* int main() { int i=square(4-1); /* It basically work as: i=4-1*4-1 Multiplication solves first so,the above statement becomes i=4-4-1 i=-1 */ printf("%d",i); return 0; }
7th Jan 2022, 6:02 AM
Muhammad Rizwan
Muhammad Rizwan - avatar
+ 1
Thank you TheWh¡teCat 🇧🇬 https://code.sololearn.com/cerU8AFJDS11
5th Jan 2022, 9:58 PM
SoloProg
SoloProg - avatar
+ 1
Add parenthesis for your code to work #define square(x) (x)*(x)
6th Jan 2022, 1:41 PM
Abraham
Abraham - avatar