|RE: Pointers to "why C behaves like that ?" email@example.com (Quinn Tyler Jackson) (2002-11-17)|
|Re: Pointers to "why C behaves like that ?" firstname.lastname@example.org (2002-11-20)|
|Re: Pointers to "why C behaves like that ?" email@example.com (jacob navia) (2002-11-24)|
|Re: Pointers to "why C behaves like that ?" firstname.lastname@example.org (2002-11-24)|
|Re: Pointers to "why C behaves like that ?" email@example.com (Joachim Durchholz) (2002-11-24)|
|Re: Pointers to "why C behaves like that ?" firstname.lastname@example.org (jacob navia) (2002-11-26)|
|Re: Pointers to "why C behaves like that ?" email@example.com.OZ.AU (Fergus Henderson) (2002-11-26)|
|[15 later articles]|
|From:||"Quinn Tyler Jackson" <firstname.lastname@example.org>|
|Date:||17 Nov 2002 23:17:12 -0500|
|Posted-Date:||17 Nov 2002 23:17:11 EST|
> > Why the C lang behaves like that:
> > We need to delare variable in advance, in contrast to other
> > lang, the program simply use without declaring it.
> suppose this code:
> void updateAccount(newAmount)
> CurrentAmount = ReadCurrentAccount(database);
> CurrentAmunt =CurentAmount+newAmount;
> You see the problem?
> A typo in the spelling of the variable "CurrentAmunt" in line 2
> creates a new variable "CurrentAmunt" that receives the
> result of the
Even if we were to have sharp eyes and catch a mistake like that,
human readers of the code would have to ask themselves "How large a
value can CurrentAmount hold?" If CurrentAmount + newAmount is greater
than that value, will CurrentAmount hold the new value?
We might say that "all undeclared variables are assumed to be of type
int" but then we have another problem: We have to look through the
headers to determine if CurrentAmount *has not* been declared, and
then, if we can't find it anywhere, assume it is an int. But what if
we couldn't find it because we didn't look hard enough, and it turns
out to be a char?
Forced declarations, besides allowing the compiler to catch typos,
allow human beings to track down the source of less obvious mistakes,
such as overflow.
Granted, some of the above *could* be automated. Something akin to
lint could catch the mispelling example above (but not all cases), as
a "dead assignment". Potential overflow can also sometimes be flagged.
But, even with a magically perfect form of lint that somehow managed
to beat the HP and could scan at the level of intent (parse what I
mean, not what I write), declarations serve as a form of code
documenation, for mere mortals. No amount of technology can do this:
int CurrentAmount1 = 0; /* current amount in the calculation of the
first account balance */
int CurrentAmount2 = 0; /* current amount in the calculation of the
second account balance */
int CurrentAmount3 = 0; /* current combined amount of accounts 1 and 2
Quinn Tyler Jackson
Return to the
Search the comp.compilers archives again.