K
Kevin D. Quitt
Eee-yup. That does tend to reduce the error listings
Curious that Mr t missed that. OTOH, maybe he didn't - I don't get his
posts for some reason.
Eee-yup. That does tend to reduce the error listings
In said:Some compilers at least, in K&R mode, complain about anything other
than int f();
But that would be misleading, given that the type of c is still char:
f.c:
#include <stdio.h>
int f(c) char c; { return printf("%d\n", (int)c); }
main.c:
int f(int);
int main() {
f(1000);
return 0;
}
I'm not sure about the behaviour of this code in K&R C, which talks about
float parameters getting widened to double, but no mention about char and
short parameters getting promoted to int.
He wasn't talking about K&R C programs, but about standard C programs
using K&R function definitions. It is perfectly legal to have a
K&R function definition and a prototype declaration for it in a standard
C program.
Dan
Eric Sosman said:J. J. Farrell said:Michael B. said:I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:
void * newKlElem (frame_size,num_blocks,num_frames,frame_locator)
size_t frame_size;
unsigned short num_blocks;
unsigned short num_frames;
Kl_frame_locator *locator;
{
[...]
The example you give is also invalid. Unless you have a prototype
in scope at the time of the call, it is not possible for your
function to receive parameters of type <unsigned short>. They will
have been promoted at the time of the call to be either <int> or
<unsigned int> depending on the type sizes in the implementation -
so to make your version portable, you need some ugly compile-time
duplication of the parameters in the function definition, or you
need to explicitly cast the parameters to an appropriate type in
each call.
This is the second time this piece of misinformation has
cropped up in this thread. I can only conclude that people
have become so accustomed to prototyped functions (that's
understandable; they *are* better) that they've completely
forgotten how old-style functions worked.
...
- Pre-Standard functions and calls work in Standard C
just as they do/did in pre-Standard C.
... Use prototypes.
Dan Pop said:I'm not sure about the behaviour of this code in K&R C, which talks about
float parameters getting widened to double, but no mention about char and
short parameters getting promoted to int.
In said:But that would be misleading, given that the type of c is still char:
[long program, reformatted a bit for vertical compression]f.c:
#include <stdio.h>
int f(c) char c; { return printf("%d\n", (int)c); }
main.c:
int f(int);
int main() {
f(1000);
return 0;
}
I'm not sure about the behaviour of this code in K&R C, which talks about
float parameters getting widened to double, but no mention about char and
short parameters getting promoted to int.
Narrow parameters behave as if converted twice, once with "the
default argument promotions", and then back to the narrow type in
the K&R function definition. Thus, this is the same as an ANSI C
program that reads, for f.c:
#include <stdio.h>
int f(int c0) { char c = c0; return printf("%d\n", (int)c); }
The same trick applies to short and float, of course.
In said:Invariably. Except, perhaps ... are there any circumstances under
which people think it's appropriate to not use prototypes? The
only one that comes to mind is a generic function pointer.
In said:As far as I know, all pre-ANSI compilers widened char and short
arguments to int (even if char was unsigned -- the first hint of
value-preserving conversion rules!).
Behavior varied for unsigned char
and unsigned short: some compilers didn't support them at all, most
compilers widened them to unsigned int, a few (or maybe just one) used
ANSI-like value-preserving rules and widened them to either int or
unsigned int depending.
Behavior also varied for the parameter
declarations -- some compilers converted the widened types back to the
declared types, other compilers quietly rewrote the parameter
declarations using the widened types (much as array declarations are
quietly rewritten as pointer declarations).
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.