0

C #preprocessing

Why the result =16? #include <stdio.h> #define sqr(x) x*x int main (){ int x=16/sqr(4); printf ("%d", x) ; }

23rd Jul 2021, 12:58 AM
Abdurrahman Azaz
Abdurrahman Azaz - avatar
3 ответов
+ 5
You can think of #define as "find and replace", it just substitutes an expression in the code with another. 16/4*4 = 16 You can try this: #define sqr(x) (x*x)
23rd Jul 2021, 2:03 AM
Tibor Santa
Tibor Santa - avatar
+ 3
make sure when defining macros, each reference to a variable is in parenthesis. It can lead to unexpected output https://code.sololearn.com/c385BV6bS3gg/?ref=app
23rd Jul 2021, 3:36 AM
Slick
Slick - avatar
+ 2
Thank you guys for explanation, I've got it
23rd Jul 2021, 9:30 AM
Abdurrahman Azaz
Abdurrahman Azaz - avatar