undefined symbol kbhit and printf()

prototype of a function in c++ is mandatory to give.........but when we give the prototype of the function then there are alot of errors.....

see ex


int printf(const char*,...);
int scanf(const cahr*,...);
int kbhit(void);
void clrscr(void);
int main(void)
{
clrscr();
printf("hello");
while(!kbhit());
return 0;
}


file extension must be .cpp


when file extension is .c or any extension except .cpp then this program gives output hello......

if u know then plz ans.........
Last edited on
Try...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifdef __cplusplus
extern "C" {
#endif

int printf(const char*,...);
int scanf(const char*,...);
int kbhit(void);
void clrscr(void);

#ifdef __cplusplus
}
#endif

int main(void)
{
	clrscr();
	printf("hello");
	while(!kbhit());
	return 0;
}


But usually you just include the function defs in stdio.h, stdlib.h, and (in your case) conio.h, which do the extern "C" for you.

Andy

P.S. The C++ compiler mangles (or decorates) names unless you tell it they're C names. And printf() etc are C names from a C library. The mangling is compiler specific and is based on the parameters types, etc. e.g. VC++ mangles int __cdecl printf(char const *,...) to ?printf@@YAHPBDZZ
Last edited on
Topic archived. No new replies allowed.