Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrong result printed on the screen when using #define SUM 1+3

Why doesn’t the code below print 16 ,0?

#define SUM 1+3
int main(void) {
    printf("%d %d\n", SUM*SUM, 4-SUM);
    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Because c treats that like x+y, not the result of that addition.
(if you want it to treat it like the result, simply put parentheses before and after the expression)

#define SUM (1+3)
int main() {
    printf("%d %d\n", SUM*SUM, 4-SUM);
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading