Re: Doubt
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Thu, 31 Jul 2008 10:57:14 +0000
Anarki said:
int main(a,b,c,d,e,f,g)
{
printf("Size of a = %d\nSize of b =%d",sizeof(a),sizeof(b));
return 0;
}
The above program perfectly compiles in cygwin using gcc
and my questions are
1.How does the compiler know what are the types of variables a, b, c,
d, e, f and g?? Where is its declaration?
In C90 (which is what you're using), the code is perfectly legal (albeit in
an obsolete style), and the compiler knows that a, b, etc are ints because
that's the default type where a type is not specified within a
declaration.
In C99, which you are *not* using but which might become popular any
century now, the code is illegal, and a diagnostic message is required.
2.I haven't included stdio.h yet the printf compiles.The compiler
doesn't complain about the requirement of function prototype, why?
In C90, no diagnostic message is required when a function is called without
a prototype being in scope (or at least, one might be, but it will be for
a reason other than the mere absence of the prototype) - the compiler will
assume that the args are fine and that the function returns int. (So if,
for example, you don't bother to include <stdlib.h> when calling malloc,
the compiler will assume it returns int, but you're assigning that value
to a pointer object, and that's why you get a diagnostic message along the
lines of "cannot convert integer to pointer without a cast" - the right
solution is *not* to add a cast, but to add a prototype!)
If the function really does return int and if you get the parameters right,
you can get away with this most of the time. In the case of variadic
functions like printf, however, the behaviour is undefined unless you
provide a prototype.
In C99, you... well, you don't necessarily need a function prototype before
calling a function (although it really really really is a good idea), but
you do at least need a function declaration before the call. Otherwise, a
diagnostic message is required.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- References:
- Doubt
- From: Anarki
- Doubt
- Prev by Date: Re: Doubt
- Next by Date: Re: x % y = ( x -(x/y) * y)
- Previous by thread: Re: Doubt
- Next by thread: Compiler warning
- Index(es):
Relevant Pages
|