"mukeshjadon34" <[EMAIL PROTECTED]> wrote:
> shivani gupta <fab_shivani@> wrote:
> > From: Brett McCoy <idragosani@>
> > > Manoj Vivek <[EMAIL PROTECTED]> wrote:
> > > > main()
> > > > {
> > > > printf("%d", main);
> > > > }
> > > >
> > > > main()
> > > > {
> > > > printf("%d", a);
> > > > }
> > > > The first snippet does not show any errors
> > > > while second one shows an error as "unknown symbol a"
> > > > ..., why please explain
> > >
> > > It should be obvious. You didn't declare a.
> >
> > i have never thought about this before...
> > main is a function name and as we know that a function
> > name cant used as identifier for variables or constants.
Says you. The following is a strictly conforming program...
#include <stdio.h>
int main()
{
int main = 42;
printf("main = %d\n", main);
return 0;
}
> > (as given in books). its really awkward to see that this
> > program display the value for main as 657 (in my pc)
> > for the other program to display value a, the error for
> > undefined has to come as its not defined .
> > probably in case of main... the value of main may be
> > defined in c or c++.
As other posts have pointed out, function names become
function pointers. It's unusual to print pointers, even
more unusual to try and print function pointers. To print
an object pointer, you can use %p, after converting the
pointer to void *. There is no conversion specifier for
function pointers. Indeed, there is not even a guarantee
that function pointers can be converted to void pointers.
> main showed some value because it is predeclared
> function by the compiler
If only that were true on real systems. If a hosted
compiler declared main, it would have to declare it as
int main() at the very least. That would stop the
plethora of void main()s you see... :-)
> it is defined by the user but its prototype is pre
> defined
The prototype is certainly not predefined (or predeclared).
There are two prototypes for main that a conforming hosted
implementation is required to support...
int main(void);
int main(int, char **);
If one was predeclared and you used the other, then the
compiler would have to issue a constraint violation for
mismatch in declarations.
So, no, main is not pre-prototyped.
But in case you still have any doubts, see 5.1.2.2.1p1:
"The function called at program startup is named main.
The implementation declares no prototype for this
function. ..."
> hence itis not giving any error
As Brett implied, it's not giving an error that main
is not declared, because main _is_ declared.
--
Peter