Re: Confusion in ANSI C's function concepts
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Wed, 24 Sep 2008 15:23:03 -0400
vaib wrote:
hi all ,
It really seems that C never ceases to amaze . All this time
i've been doing C and i thought i was quite adept at it but i was
wrong . So without wasting any more time , here's the confusion .
I read in K&R that ANSI introduced the concept of function
prototyping in C and it was missing there initially ( it borrowed the
concept from C++ ) _but_ it did it make it compulsory to actually
include the function declaration in the program , the reason being -
so that older C code ( the ones missing the declarations ) could
run on newer compilers too .
Partially right, but mostly confused. Starting with the original
ANSI "C89" Standard:
1) Function definitions can use prototypes, or they can use the
older, non-prototyped "K&R-style" form.
2) A function definition also serves as a declaration, so if the
definition is in the same translation unit as the calls and
precedes them, no separate declaration is needed.
3) If the function definition is in a different translation unit
or appears after some of the calls, you may want or sometimes
need to write a declaration for the function.
4) If a function is called without a prior declaration, it is
assumed to take a fixed (but unknown) number of parameters,
all of "promoted" types, and to return an int. If the function
is variadic or is defined with a prototype that uses "promotable"
types or does not return an int, the no-declaration call is
erroneous.
The later "C99" Standard changed the rules a little: Functions
must now be declared before use, always. For C99, delete "may want
or sometimes" from (3), and delete (4) altogether.
So the situation now is this - if there
is no function declaration corresponding to the function call and the
call does not say anything about the return type then the compiler
should assume a declaration with an int return type . for example ,
main()
{ fun(3) ;
return 0 ;
}
in this case the compiler assumes this declaration - int fun( the
standard says nothing about the parameters so thats implementation
dependent ) ;
Close, but still no cigar. C89's implied declaration says that
the function returns an int and has a fixed number of parameters,
but does not say what that number is nor what the parameters' types
are. And under C99 rules, a diagnostic is required (because main()
is defined with no type, and because fun() is used without being
declared).
and the code compiles without any hitch .
my question is what sort of declaration would the
compiler assume in the following case :
main()
{
void * p ;
p = fun(3) ;
}
in the above case the return type is mentioned as void * .
Under C89 rules, fun() would be assumed to return an int. A
diagnostic is required, because an int value (other than a constant
zero) cannot be converted to a pointer without a cast. The fact
that you assign a function's value to a variable of a particular
type says nothing about the type of the function's value: For
example, `double d = fun(3);' does not mean that fun() returns a
double, but that fun() returns an int which is then converted to
a double.
Under C99 rules, as before, a diagnostic is required.
also what happens in this case :
main()
{
void * p ;
p = malloc(sizeof(int)) ;
return 0 ;
}
the above code also compiles and _executes_ successfully on
gcc .
It shouldn't. Since malloc() has not been declared, it is
assumed to return an int and the compiler should object to your
attempt to convert that int to a pointer. A diagnostic is required;
I speculate that you are running gcc in some kind of lenient mode.
few more regarding these concepts have cropped up lately in my mind
but i'll ask them as the thread proceeds .
Best practice: Declare all functions before use, even when C89
would let you get away without it, and use prototypes in all function
definitions and declarations. Also, get in the habit of running gcc
with the "-W" and "-Wall" flags; "-ansi" (or "-std=xxx") and "-pedantic"
are also recommended. Saves a lot of worry and trouble.
--
Eric.Sosman@xxxxxxx
.
- References:
- Confusion in ANSI C's function concepts
- From: vaib
- Confusion in ANSI C's function concepts
- Prev by Date: Re: Highly efficient string reversal code
- Next by Date: The Second Web | A Whole New Internet ?!
- Previous by thread: Re: Confusion in ANSI C's function concepts
- Next by thread: Re: Confusion in ANSI C's function concepts
- Index(es):
Relevant Pages
|