Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Here is a neat trick for checking whether two strings are equal
Q: Here's a neat trick for checking whether two strings are equal:
if(!strcmp(s1, s2))
Is this good style?
✍: Guest
It is not particularly good style, although it is a popular idiom. The test succeeds if the two strings are equal, but the use of ! (``not'') suggests that it tests for inequality.
Another option is to define a macro:
#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
which you can then use like this:
if(Streq(s1, s2))
Another option (which borders on preprocessor abuse;is to define
#define StrRel(s1, op, s2) (strcmp(s1, s2) op 0)
after which you can say things like
if(StrRel(s1, ==, s2)) ...
if(StrRel(s1, !=, s2)) ...
if(StrRel(s1, >=, s2)) ...
2015-05-15, 1522👍, 0💬
Popular Posts:
Describe different elements in Static Chart diagrams ? Package: - It logically groups element of a U...
How To Compare Two Strings with strcmp()? - PHP Script Tips - PHP Built-in Functions for Strings PHP...
How can a servlet refresh automatically if some new data has entered the database? You can use a cli...
Can you explain in GSC and VAF in function points? In GSC (General System Characteristic) there are ...
How can I execute a PHP script using command line? Just run the PHP CLI (Command Line Interface) pro...