Precedent for use of =

Dave Haynie daveh at cbmvax.cbm.UUCP
Thu Jul 10 04:24:20 AEST 1986


> 
> In article <499 at cbmvax.cbmvax.cbm.UUCP> daveh at cbmvax.cbm.UUCP (Dave Haynie) writes:
>>>   if (i = 0) {
>>>     /* do something */
>>>   }
>>>   else {
>>>     /* do something else */
>>>   }
>>> 
>>> is legal C and usually  /* does something else */   than you expected  :-)
>>
>>As long as you're writing in C, and you REALLY know the language, the above
>>construct would be ridiculous.
> 
> I disagree and I believe you do, too. If you really look for this
> construct, it is quite common in C, altho good programmers comment
> that they know what they're doing here... for example, my favorite:
> 
> main(argc, argv)
> char **argv;
> {
>     char *prog_name, *rindex();
> 
>     if (prog_name = rindex(*argv, '/'))  /* find last '/' in argv[0] */
> 	prog_name++; /* set prog_name to string following last '/' */
>     else prog_name = *argv; /* program was exec-ed from same dir or in PATH */
>     /* etc... */
> }
> 
> This sort of thing is also quite common when using other string(3) routines
> or basically anything which returns char *
> I like that much better than doing the same thing via:
> 
>     prog_name = rindex(*argv, '/');
>     if (!prog_name)
> 	prog_name = *argv;
>     else prog_name++;
> 
> I don't like this method as well simply because there are more statements
> than needed and the previous version isn't that cluttered. Since the
> nature of this discussion is the use (or misuse) of the = operator, I
> would say that C was designed more robustly simply because you can do
> the above whereas PASCAL won't let you. C gives you the choice of doing
> it if you prefer to and to do it the other way if you really want to (or
> don't know better).
> 

Whoa, there!  I think we're in agreement here, believe it or not.  I've
commented before on the great advantages of the assignment OPERATOR in C,
which is what you've shown.  The case initially presented above is the
trivial case, which is ridiculous, since the /* do something */ clause will
never be executed.  The example that you show is good C, I use that style
all of the time, and a good compiler will produce code for it that's more
efficient than in the second example.  There's no confusion in the if ()
clause of your first example, since it would be silly to expect an 
equality test there, with prog_name being an uninitialized variable (in
fact, the Lattice C compiler I use on the Amiga would generate an
"uninitialized auto variable" warning if I used an == operator there
instead of the assignment operator.

>>I think that most of the folks that are
>>unhappy with the way that C handles = and == are frustrated Pascal hackers
>>who can't quite adjust to the power and terseness of C.  Maybe if they'd
>>spend a few extra hours LEARNING C instead of trying to write Pascal in C,
>>they'd be much better off. [etc..]
> 
> I quite agree.  When I used to work at my school helping students, I found
> that the students who complained about C were those who just wanted to take
> the course to get out of the requirement (elective general ed for most).
> Other comp sci majors who complained about C (either in favor of PASCAL or
> not) were merely pedantic and just wanted their peers to respect them.
> I even found that the comp sci faculty that pushed PASCAL knew little about
> C.  Although all of the faculty (whether they kenw C or not) agreed that
> PASCAL was the correct language to learn for freshmen (I am undecided about
> this), most of them agreed that it was too limiting for the upper-division
> courses which concentrated on more important issues.
> 
> dan (argv at sri-spam.arpa)
-- 
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Dave Haynie    {caip,ihnp4,allegra,seismo}!cbmvax!daveh

	"I don't feel safe in this world no more, 
	 I don't want to die in a nuclear war,
	 I want to sail away to a distant shore
	 And live like an ape man."
				-The Kinks

	These opinions are my own, though for a small fee they be yours too.
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



More information about the Comp.lang.c mailing list