Some Comments & Questions about ANSI C
Geoff Rimmer
geoff at cs.warwick.ac.uk
Mon Jun 12 08:19:24 AEST 1989
1. 15 standard include files are listed in K&R2. Only 12 are
described in sufficient detail. The other three are
errno.h - this will contain the #defines for error messages.
But will it contain the declaration "extern int errno" ?
locale.h - This will contain "properties that depend on local
language, nationality, or culture". Such as?
stddef.h - Definition of the type size_t (A7.4.8). But what
else goes in here? ino_t and dev_t presumably stay
in sys/types.h (8.6 p 181).
2. It seems to me that every time I want to #include <stdio.h>, I must
also #include <stdarg.h> and <stddef.h> beforehand. This is to stop
it barfing on the function declarations in <stdio.h> like
size_t fread( void *, size_t, size_t, FILE * );
/* size_t is defined in <stddef.h> */
and
int vprintf( const char *, va_list );
/* va_list is defined in <stdarg.h> */
I find this annoying sometimes, especially when working on a simple
program that is only including <stdio.h> to get _iobuf defined. Am I
correct on this?
3. Am I missing something, or is "difftime" the most simple function
around? It seems to me that it is essentially a subtract:
double difftime( time_t t2, time_t t1)
{
return (double)t2 - t1;
}
In which case, why is it a function? I'd rather do a subtraction myself!
4. I'm still not entirely sure I understand "void *". If I have a
function which takes as argument a void *, and I want to pass it the
variable x (which could be of type char *, int *, or even
float(*)(int,char*) ), do I have to CAST x to a (void *) when calling
the function? Similarly, if the function then returned a void *, and
I have a variable y of type int *, would I have to do
y = (int *) function (......);
?
5. Does the standard say anything about where function definitions for
fopen and opendir must go? Also, is <dirent.h> mentioned in the
standard, or is it up to the individual implementor to choose this?
6. Finally, I see there's no strdup in ANSI C. Sigh. I guess it's
back to
#define strdup(x) (strcpy(Malloc(strlen(x)+1),(x)))
:-(
Geoff
One of these days, I'm going to write an ANSI C compiler. And
whenever it encounters some code whose behavior is undefined
(such as fflush(stdin);) it will draw a picture of Snow White (tm)
on the screen, and then play Rule Brittania 3 times, before
crashing with a segmentation fault, and leaving a
while(1){fork();malloc(UINT_MAX);}
running in the background.
/---------------------------------------------------------------\
| GEOFF RIMMER |
| email : geoff at uk.ac.warwick.cs |
| address : Computer Science Dept, Warwick University, |
| Coventry, England. |
| PHONE : +44 203 692320 |
| FAX : +44 865 726753 |
\---------------------------------------------------------------/
"No representation without taxation!"
- Rik Mayall, The New Statesman.
More information about the Comp.lang.c
mailing list