How this works?

I have a program............

#include<stdio.h>
#include<unistd.h>
main()
{
if(fork == 0)
{
printf("Hi every body:p!!!!!!!!!!");
}
}

This program works with out any error. here fork is not a system call. It just act as a variable.But how it works without declaring it? What data type it is?

Please help me to get a solution. I have tried all . But i cant come out with an answer.:frowning:

you remove the #include<unistd.h> header file then see what error it is saying.

see the following example.
in aa.c file content is

int ss=2;  

then I include the file in the following program.

#include<stdio.h>
#include "aa.c"
main()
{
        printf("%d",ss);
}                                                                                                                            

it will work. but I didn't declare the ss in this file.

how it's work? did you understand this example with your code.

the fork variable is declared in #include<unistd.h> file. so it's not showing the error.

if you are using vim. editor then place the cursor on the "#include<unistd.h> " and hit gf then the "unistd.h" file will open. then search the variable fork.

1 Like

But i dint find any declared variable like fork in unistd.h. In your example i can tell ss is an integer type by seeing the include header file..but here can you tell wat datatype fork is? And in a declared variable i can do ++ or -- operation. but here its not working.:confused:

No fork in unistd ?

$ grep -w fork /usr/include/unistd.h
extern pid_t fork(void);
extern pid_t fork();

her fork is not a function or system call:rolleyes:

so what ?

fork in this case is not a variable. It's a pointer of type void to the entry address of the fork() function in the C library. Basically, you could "rename" the fork call by doing

__pid_t (*foo)() = fork;

and later calling foo instead of fork.

Also, if you'd have compiled with warnings enabled, or read them, you'd have seen something like this:

warning: the address of �fork� will never be NULL
1 Like

Indeed, it is a constant.

What compiler gives you that warning ?
Neither gcc with most warnings enabled (-Wall), nor Sun cc seem to choke on that code.

But while checking the sizeof() to fork its showing 1.:mad:

Depends on the compiler. Gcc indeed (wrongly) thinks fork size is 1 but cc is complaining:

"a.c", line 5: cannot take sizeof function: fork
1 Like

Compiled with gcc. And it didn't choke on it, it was just a warning (-Wall)

1 Like

Broadly speaking, it "works" because variables and functions are both just memory locations; the difference is what's stored there and how it's used.

1 Like

Got it. This warning appears with ggc4 but not gcc3 which I was testing. The sizeof bug is still there with gcc4 though.

1 Like

Seems this is a deliberate extension on the standard: Using and Porting the GNU Compiler Collection (GCC): C Extensions. With -pedantic it will complain about it.

Thanks for the link, not a bug but a typical gnu style feature .

But while checking the size of the fork its giving 1. Why?
:confused:

But when i check the size of fork its giving 1. If it is pointer how it could be

---------- Post updated at 09:58 AM ---------- Previous update was at 09:55 AM ----------

Thank you guys !!!!!!!!!!!!.

It's not always a pointer. If you took the address of it with & it would always be a pointer.

Editing this, hold on.

1 Like

If i have less programming sense,what to call the people who has replied to my post?.Learn how to behave in a public forum.Respect others.It just to help the people:)