From http://c-faq.com/style/strcmp.html, I learned the following convenience macro:
#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
I want to know why there are so many parentheses being used in this macro. Is each parenthesis serving a purpose or is this macro using redundant parentheses that serve no purpose?
Can we remove the parentheses around s1
and s2
and make a macro like this?
#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)
The MyStreq
macro seems to work for me as nicely as Streq
.
#include <string.h>
#include <stdio.h>
#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)
int main()
{
printf("%d %d\n", Streq("foo", "foo"), MyStreq("foo", "foo"));
printf("%d %d\n", Streq("fox", "foo"), MyStreq("fox", "foo"));
printf("%d %d\n", Streq("foo", "fox"), MyStreq("foo", "fox"));
return 0;
}
Output from the code above:
1 1
0 0
0 0
Can you imagine any usage of these macros where Streq
does what one would expect but MyStreq
does not?