printf error

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
printf lives in cstdio. std::cout lives in ostream.

You must declare the functions by including appropriate headers.
Last edited on
Just put #include <stdio.h> at the top of your code.
Last edited on
Looks like C code, so I would say #include <stdio.h>
i compile the program using g++.


So you'd want <cstdio> wouldn't you?
Thanks very much everyone for the reply ! yeah, with printf i had to include stdio header or with cout the iostream header.
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
Topic archived. No new replies allowed.