Mar 6, 2011 at 5:28pm UTC
Hello friends,
i have a code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(void ){
pid_t h;
h = fork();
if (h==0) {
printf("Hello i am child %d" );
}
else if (h>0) {
printf("Hello i am parent %d" );
}
else {
printf("fork error!\n" );
return 0;
}
}
return 0;
}
and i would like to ask:
i receive an error from the compiler:
`printf' was not declared in this scope
, any idea how to solve it. (i tried using cout, but i still receive the same error). i compile the program using g++.
Any help will be appreciated.
Thank you.
Last edited on Mar 6, 2011 at 6:49pm UTC
Mar 6, 2011 at 6:11pm UTC
printf
lives in cstdio. std::cout
lives in ostream.
You must declare the functions by including appropriate headers.
Last edited on Mar 6, 2011 at 6:14pm UTC
Mar 6, 2011 at 9:05pm UTC
Just put #include <stdio.h>
at the top of your code.
Last edited on Mar 6, 2011 at 9:53pm UTC
Mar 6, 2011 at 9:20pm UTC
Looks like C code, so I would say #include <stdio.h>
Mar 6, 2011 at 10:36pm UTC
Thanks very much everyone for the reply ! yeah, with printf i had to include stdio header or with cout the iostream header.
Mar 6, 2011 at 11:26pm UTC
Didn't notice that line. In that case, you ought to also use cstdlib instead of stdlib.h
Though TBH, if you are writing C++ you should just use the C++ libraries instead of the C standard library.
Last edited on Mar 6, 2011 at 11:26pm UTC