Question 17.3

Here's a neat trick for checking whether two strings are equal:

if(!strcmp(s1, s2))
Is this good style?


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.

A better option is to use a macro:

	#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)

Opinions on code style, like those on religion, can be debated endlessly. Though good style is a worthy goal, and can usually be recognized, it cannot be rigorously codified. See also question 17.10.


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback