Re: Interesting code




Richard Heathfield wrote:
DhaneshNair@xxxxxxxxx top-posted, but I've fixed that. He said:

v4vijayakumar wrote:
Happened to see my old (last millennium) c code. Almost forgot that I
wrote it .
It is interesting. :-)

int sqrt(int no)
{
int t;
no = t * t;
return t;
}

Can u tell me what is interesting in this code ??

No, there isn't any regular comp.lang.c contributor called "u".
(Translation: abbreviating "you" to "u" marks you out as someone who can't
even spell "you" properly. Take the time to type "you" in full, in the
interests of clear communication and of not looking idiotic.)

Here are the principal points of interest:

1) function name choice invades implementation namespace, thus invoking
undefined behaviour;
2) function name choice gives a misleading impression of what the function
tries to do;
3) t's value is indeterminate, but the function still attempts to use t's
value regardless, thus invoking undefined behaviour;
4) a parameter is used solely for assigning a value to, and this value will
be lost at the same time that the parameter itself is lost, at the end of
this function;
5) an indeterminate value is returned from this function, thus invoking
undefined behaviour in any caller that evaluates it;
6) even if all those faults were fixed, the function would still need major
repair work (type choices, range validation, etc).

Summary: it was written by someone who didn't know C very well six or seven
years ago. Whether they have learned their lessons in the meantime, I
cannot say.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.

You take the fun out of the post. The post was not asking any help or
correction in the code. It is just for fun.

I wrote (1999) this to find square root of a number. I was then new to
c. I got wrong results and didn't know why it was not working. Then,
someone told me about the sqrt library function. Anyhow I saved the
code for future reference (?) and I forgot about it. Suddenly happened
to see the code and recollected the past.

.



Relevant Pages

  • Re: Interesting code
    ... int sqrt ... function name choice invades implementation namespace, thus invoking ... value regardless, thus invoking undefined behaviour; ... adjust your email clients. ...
    (comp.lang.c)
  • Re: Interesting code
    ... Richard Heathfield wrote: ... function name choice invades implementation namespace, thus invoking ... value regardless, thus invoking undefined behaviour; ... adjust your email clients. ...
    (comp.lang.c)
  • Re: idiom related
    ... Richard Heathfield writes: ... Undefined behaviour if dest is not properly aligned for an int. ... Lets say an array of characters has ...
    (comp.lang.c)
  • Re: Increment operator
    ... Richard Heathfield writes: ... int i = 3; ... Yes, which is a good argument for expecting 3, but presumably the OP's ... Undefined behaviour is not /required/ to be bizarre behaviour. ...
    (comp.lang.c)
  • Re: Works on Windows but not on HPUX, Help Please
    ... do I read C89 correctly that the char strwill be ... converted to an int, not an unsigned int, even if char is unsigned, ... unsigned short can also be promoted to signed int. ... if char is unsigned then there is undefined behaviour ...
    (comp.lang.c)