0

I have a small but confusing problem... First question is what is the use of main.I know the question is silly and simple but i have a problem.I have written a code

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>


int main(){
pid_t ty;

ty=fork();
if(ty==0){
const char* x="/home/brucewilson/Desktop/jack_sahoo_teja_CDP/hey2";
static char *argv[]={"echo","Foo is my name.",NULL};
int main(){//observe this is second main in my child process
printf("hello");
}
int add(){
printf("5");
}

main();
add();

}

}`

Did you observe the second main function i used in my child process well the compiler gave me no error for this..Added to this it gave me the output as "hello" and 5.

And obviously the below code would give an error...

int main(){

printf("main");
main();
}
int main(){
}

So my question is why did it work for the child process?So is the notion that im assuming to be true that is no function can be named main() and every child process will have a main function shared from its parent is false.Please explain me what is going underneath this code inside my system because the child process assumes main just as another function and it doesnot need a main function also.Then how will the child process know from where should it start?

jack wilson
  • 145
  • 1
  • 13
  • You say the compiler isn't giving you errors? I'm getting quite a few when I try to compile your code. – CLL Mar 14 '15 at 06:42
  • For the second code you do get errors for the first you dont..If u want to see it i cant post u a screenshot – jack wilson Mar 14 '15 at 06:44
  • I definitely get many compiler errors for the first code. For instance, you can't declare functions within other functions in C++. The language doesn't support that feature. See: http://stackoverflow.com/questions/4324763/c-can-we-have-functions-inside-functions – CLL Mar 14 '15 at 06:52
  • please see the answer below it depends on your compiler version.So that is the reason we are getting conflicts in our copilation – jack wilson Mar 14 '15 at 06:59
  • If you are using a non-standard extension, it helps to mention these things in your question - just for future reference. – CLL Mar 14 '15 at 07:01
  • okay i will do it the next time thnq for reference – jack wilson Mar 14 '15 at 07:10

1 Answers1

2

You are using a non-standard GCC extension known as 'nested functions'.

You second example fails because you aren't nesting the second definition of main() so it conflicts with the first one.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • ok sir can you explain me a bit more in detail as what is actually happening.I understood by your link that i can use same name so is it that the main function written next is treated as a local function? – jack wilson Mar 14 '15 at 06:56
  • I'm not sure I can go into much more detail than is in the linked documentation. After the nested function is declared, calls to that function's name *in that scope* will call the nested function. – Michael Burr Mar 14 '15 at 07:01
  • so now is it that child as well as parent are having a same main function? – jack wilson Mar 14 '15 at 07:03
  • What's happening here has nothing in particular to do with the child/parent situation created by `fork()`. After a fork you have the same program running as two separate processes with mostly, but not entirely, the same state. Get rid of the `fork()` call in your program and replace it with `0` - you'll see the same output. Essentially the same thing is happening, just in a single process instead of two. – Michael Burr Mar 14 '15 at 07:08
  • 1
    @jackwilson Let me make this simple: **don't use nested functions**. They are a glitchy, nonstandard language extension implemented only by GCC. You don't really need to know anything about them, besides that you shouldn't use them. –  Mar 14 '15 at 07:08
  • @duskwuff: I like this quote from the docs: "If you try to call the nested function through its address after the containing function exits, all hell breaks loose". – Michael Burr Mar 14 '15 at 07:13