From a5c23e8af4be114ed5e04c1034acbc6ef4890719 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Tue, 12 Sep 2017 22:42:18 -0700 Subject: Update c.html.markdown Note on macro hygiene. Fixes #2853 --- c.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 1ad0dd53..c47a2df7 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -756,8 +756,14 @@ as the C file. /* Like c source files macros can be defined in headers and used in files */ /* that include this header file. */ #define EXAMPLE_NAME "Dennis Ritchie" -/* Function macros can also be defined. */ -#define ADD(a, b) (a + b) + +/* Function macros can also be defined. */ +#define ADD(a, b) ((a) + (b)) + +/* Notice the parenthesis surrounding the arguments -- this is important to */ +/* ensure that a and b don't get expanded in an unexpected way (e.g. consider */ +/* MUL(x, y) (x * y); MUL(1 + 2, 3) would expand to (1 + 2 * 3), yielding an */ +/* incorrect result) */ /* Structs and typedefs can be used for consistency between files. */ typedef struct Node -- cgit v1.2.3