#include
int main(int argc, char** argv) {
if (argc == 0) {
return 0;
}
else {
printf("%s\n", argv[0]);
main(argc - 1, &(argv[1]));
}
}
Compile it and try:
$ ./a.out a b c d
./a.out
a
b
c
d
geekin'
#include
int main(int argc, char** argv) {
if (argc == 0) {
return 0;
}
else {
printf("%s\n", argv[0]);
main(argc - 1, &(argv[1]));
}
}
$ ./a.out a b c d
./a.out
a
b
c
d
1 comment:
Sure you can, main() is just an ordinary function. It's not even the first function that gets invoked when your program runs. Lots of stuff happens first! The (g)libc does all sorts of crazy initialization stuff like executing constructors from the .ctors section before it even thinks about calling main.
The actually entry point is typically called _start (listed in the ELF program headers) which calls __libc_startmain which trampolines into the glibc initialization code (_dl_runtime_resolve), and on and on. I can't find an easy way to count instructions/functions called before main() gets invoked, but it's pretty seriously hairy.
Post a Comment