R
Ron Ford
Is qsort an intrinsic for C?
qsort() is a function in C's Standard library, available
for use in all hosted implementations.
What's an "intrinsic?"
An intrinsic is something that comes with the food in a given syntax.
One of my intrinsics is that I like Hillary. Your mileage may vary.
Nonsense.
I don't see qsort in K&R as I leaf through or in the index. Did I
photograph the wrong target when I thought to have read of qsort in K&R?
No it's not.
Photograph the wrong target? nonsense.
Which K&R? See Appendix B in K&R2.
In hex maybe, but certainly not in decimal.Ron said:The appendices in K&R are numbered.
If by "number" you mean A, B, and C then yes. My own K&R second edition
shows nice "qsort function" and "qsort library function" lines in the
index at the end of the book.
What's an "intrinsic?"
So, no, qsort is not an intrinsic; if it were you wouldn't need
to include stdlib.h nor would you need to link in the standard
library.
Nor could you write your own qsort and use it, either by omitting to
include stdlib.h, or by simply replacing qsort in the standard library.
You can have intrinsics and still have a standard library. Some
languages do. Regardless, if something is an intrinsic you don't
need a library to get it.
Guess what: despite the shoot-your-own-foot permission some
implementations may give you, the Standard doesn't guarantee that you
can [use your own qsort], either.
Quite true, but beside the point. It is not even potentially
possible for intrinsics, since they aren't in libraries.
That last seems like an odd statement to me, given that (e.g.) the
Fortran systems I have used have relied on a library (the one
automatically included by the "fort" or "f66" or "f77" or whatever
front-end command) to supply the code for most of the various
intrinsics.
(For that matter, the "standard C library" normally does not require
special link options when using the usual front-end commands. The
"-lm" flag to cc/gcc is the glaring annoying exception to this rule.)
Guess what: despite the shoot-your-own-foot permission some
implementations may give you, the Standard doesn't guarantee that you
can [use your own qsort], either.
Quite true, but beside the point. It is not even potentially
possible for intrinsics, since they aren't in libraries.
Again, in a number of those Fortran systems, it was easy to use
your own code for intrinsics, simply by adding the appropriate
object or library file to the link line (as the default Fortran
library was linked at the end). As in C, this was rarely if ever
officially supported, and sometimes the call you expected got
short-circuited, so (just as with C systems) you had to know what
you were doing. But it was more than just "potentially possible":
sometimes we even did it (to use special instructions, or work
around an implementation bug).
Thanks, Chris, there is definitely a useful contrast here between C and the
common extension that is fortran. I'm reconfiguring my mingw C capability
but think I can call qsort from another syntax. Since C is likely to be
under the hood of many syntaxes that can call C, I wanted to see how to do
this thing that I think is important.
Calling it from a place without every C99 guarantee is a bit of a trick:
%- > Is qsort an intrinsic for C?
%-
%- Yes, but despite the name it isn't guaranteed to
%- implement quicksort.
%-
%- The advantage is that it can sort an array of any kind
%- of data structure given a pointer to the array (usual
%- for C), the length, the length of each array element
%- (which must be in contiguous storage), and a comparison
%- function. The comparison function is given pointers to
%- two elements and returns a positive, zero, or negative
%- value if the first is greater then, equal to, or less
%- than the second. With the appropriate compare function
%- the array can be an array of pointers, or structures
%- containing pointers.
%-
%- To do that in Fortran 2003 might also require a routine
%- to copy such array elements.
Is Glen (quoted) correct?
I guess the notion of keyword is less sacred in the non C world.
They say you just change what the linker finds first.
Eric Sosman said:Yes, but this isn't comp.lang.fortran nor alt.st.elsewhere.
Also, you will find that the word "intrinsic" appears nowhere in
the C Standard, not even in non-normative text. I asked the
question to learn what "Ron Ford" thought the word meant, but
learned instead that "Ron Ford" is not worth paying any heed to.
Richard Harter said:Quite true; still it isn't really necessary in comp.lang.c to
pretend that the rest of the world doesn't exist. If someone
asks if you can do closures in C, it's not much of an answer to
say that closures aren't defined in the standard.
Don't you think now might be a good time to do some growing up?
The nice thing about growing up is that you might be able to hold down
a job.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.