how #define a main() function and call our own main function?

A

Army1987

[snip >100 lines]
How long are you two going to continue to quote entire posts?
Does anybody pays you a cent for each millisecond anyone takes to
scroll down?
No. The main() function cannot be called from a user defined library.
The main() function is special. It defines the entry point for
program startup.

army1987@army1987-laptop:~$ cat foo.c myfunc.c
/* Begin foo.c */
#include <stdio.h>
extern void myfunc(void);
int main(int argc, char *argv[])
{
if (argv == (char **)0 && argc < 0)
puts("hello, world");
else
myfunc();
return 0;
}
/* End foo.c */

/* Begin myfunc.c */
#include <stdio.h>
extern int main(int argc, char *argv[]);
void myfunc(void)
{
puts("Hey, I'm myfunc().");
main(-1, (char **)0);
}
/* End myfunc.c */
army1987@army1987-laptop:~$ gcc -ansi -pedantic -Wall -Wextra -O3 myfunc.c -c
army1987@army1987-laptop:~$ gcc -ansi -pedantic -Wall -Wextra -O3 myfunc.o
foo.c
army1987@army1987-laptop:~$ ./a.out
Hey, I'm myfunc().
hello, world
army1987@army1987-laptop:~$
 
D

Default User

user923005 wrote:

[Gigantic snip]

Dann, there were over 170 lines of quotes in your message. Is there
some reason you thought that was appropriate?




Brian
 
M

Mark McIntyre

int main(int argc)
{
printf("Hello World\n");
return 0;
}
I am able to compile and run this.

You need to turn on your compiler warnings. Read the manual to find
out how to make it warn you properly.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Richard Heathfield said:
Army1987 said:

int main() is C99-conforming.

But 'main()' isn't.

(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming. The debate depends on some extremely
picky analysis of the wording of the standard. In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)
 
R

Richard Heathfield

Keith Thompson said:
But 'main()' isn't.

Yes, it's astonishing how powerful implicit int is. I could have sworn I'd
read int main(), not just main().
(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming.

There is? I had no idea. Please do tell.
The debate depends on some extremely
picky analysis of the wording of the standard.

Picky works for me, provided it's good picky. But which bit are you talking
about?
In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)

Agreed.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said: [...]
(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming.

There is? I had no idea. Please do tell.
The debate depends on some extremely
picky analysis of the wording of the standard.

Picky works for me, provided it's good picky. But which bit are you talking
about?
In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)

Agreed.

I don't remember the details off the top of my head, which is *part*
of the reason I didn't explain further. I'll have to either track
down the discussion or recreate it.
 
R

Richard Heathfield

Keith Thompson said:

I don't remember the details off the top of my head, which is *part*
of the reason I didn't explain further. I'll have to either track
down the discussion or recreate it.

Well, I'm not particularly fussed about it, so don't put yourself out on my
account - but my own take is that I would *not* like to have to defend the
proposition that int main() is non-C99-conforming.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top