Re: Doubt



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
.



Relevant Pages

  • Re: C Questions
    ... No prototype is given for this function, so the compiler cannot ... To prototype a function with no parameters, use void: ... %d takes an int argument, ...
    (comp.lang.c)
  • Re: program having printf() function invocation without including
    ... warning and would assume that it would return a int, ... The prototype is primarily for the compiler's benifit, ... It helps the compiler to output the ... the label 'printf'/'_printf' and link in the object code. ...
    (comp.lang.c)
  • Re: To coerce or not...? struct sockaddr vs struct sockaddr_in
    ... that "pointer to struct X" and "pointer to struct Y" are not ... prototype, if it has one -- and apparently it does on your system ... this instructs the compiler to truncate the ".14" part, ... Grabbing an "int", when the compiler passed ...
    (comp.lang.c)
  • Re: derangement: coding review request
    ... > The declaration or prototype tells the compiler what type of arguments to ... > an int then feeding it a pointer will cause it to fetch an int from the ... > club tie and pouring brake fluid on his porsche. ...
    (comp.lang.c)
  • Re: The importance of prototypes
    ... >> eliminating compiler warning messages). ... I added the prototype and the problem went away. ... >> integer arguments and thus puts in a type cast for me? ... > argument, and you pass it an int, you must cast the int to double. ...
    (comp.lang.c)