This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients corona10, erlendaasland, vstinner
Date 2021-03-15.14:12:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1615817550.47.0.575241129661.issue43502@roundup.psfhosted.org>
In-reply-to
Content
For me, one of the my worst issue is when a macro evalutes an argument twice.

Example:
---
#include <stdio.h>

#define DOUBLE(value) ((value) + (value))

int main()
{
    int x = 1;
    // expanded to: ((++x) + (++x))
    int y = DOUBLE(++x);
    printf("x=%i y=%i\n", x, y);
    return 0;
}
---

Surprising output:
---
$ gcc x.c -o x; ./x
x=3 y=6
---

Expected output:
---
x=2 y=4
---

Fixed code using a static inline function:
---
#include <stdio.h>

static inline int DOUBLE(int value) {
    return value + value;
}

int main()
{
    int x = 1;
    // expanded to: DOUBLE(++x)
    int y = DOUBLE(++x);
    printf("x=%i y=%i\n", x, y);
    return 0;
}
---

Output:
---
$ gcc x.c -o x; ./x
x=2 y=4
---
History
Date User Action Args
2021-03-15 14:12:30vstinnersetrecipients: + vstinner, corona10, erlendaasland
2021-03-15 14:12:30vstinnersetmessageid: <1615817550.47.0.575241129661.issue43502@roundup.psfhosted.org>
2021-03-15 14:12:30vstinnerlinkissue43502 messages
2021-03-15 14:12:29vstinnercreate