Precedent for use of =

AAAARRRRGGGGv argv at sri-spam.ARPA
Wed Jul 9 12:03:57 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).

>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)



More information about the Comp.lang.c mailing list