Compiling the following problem with visual studio 2000 gives the errors:

temp.obj : error LNK2001: unresolved external symbol "void __cdecl ungetch(int)" (?ungetch@@YAXH@Z)

temp.obj : error LNK2001: unresolved external symbol "int __cdecl getch(void)" (?getch@@YAHXZ)

Debug/temp.exe : fatal error LNK1120: 2 unresolved externals

Error executing link.exe.

The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).

#include<stdio.h>
#include<ctype.h>

int getch(void);
void ungetch(int);

int getint(int *pn)
{
	int c, sign;
	
	while (isspace(c=getch()));

	if(!isdigit(c) && c != EOF && c != '+' && c != '-')
	{
		ungetch(c);
		return 0;
	}
	
	sign = ( c == '-') ? -1 : 1;
	
	if( c== '+' || c == '-')
		c = getch();
	
	for( *pn = 0; isdigit(c); c = getch())
		*pn = 10 * *pn + (c - '0');
	
	*pn *= sign;

	if (c != EOF) ungetch(c);

	return c;
}

int main()
{
	int *p;

	p = new int[10];

	p[0] = 4;
	p[1] = 2;
	p[2] = 6;
	p[3] = 8;
	p[4] = 2;
	p[5] = 1;
	p[6] = 7;
	p[7] = 1;
	p[8] = 2;
	p[9] = 1;

	int i = getint(p);

	printf("The value is %d", i);
		
	return 0;
}

Thank you ,daviddoria , But can you give me a hint 'bout how to run the program .... I mean how to pass the parameter to the function?? I run the program and whatever I do I get the out put of

The value is 13

main() is passing an array of ints to that function, but the function is using it like its a pointer to a single int.

>>The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).
I doubt it -- he is a hell of a lot better programmer than that!

Compiling the following problem with visual studio 2000 gives the errors:

temp.obj : error LNK2001: unresolved external symbol "void __cdecl ungetch(int)" (?ungetch@@YAXH@Z)

temp.obj : error LNK2001: unresolved external symbol "int __cdecl getch(void)" (?getch@@YAHXZ)

Debug/temp.exe : fatal error LNK1120: 2 unresolved externals

Error executing link.exe.

The code is taken from Dennis Ritchie's "C Programming Language" 's page 97 (2nd Edition).

#include<stdio.h>
#include<ctype.h>

int getch(void);
void ungetch(int);

int getint(int *pn)
{
	int c, sign;
	
	while (isspace(c=getch()));

	if(!isdigit(c) && c != EOF && c != '+' && c != '-')
	{
		ungetch(c);
		return 0;
	}
	
	sign = ( c == '-') ? -1 : 1;
	
	if( c== '+' || c == '-')
		c = getch();
	
	for( *pn = 0; isdigit(c); c = getch())
		*pn = 10 * *pn + (c - '0');
	
	*pn *= sign;

	if (c != EOF) ungetch(c);

	return c;
}

int main()
{
	int *p;

	p = new int[10];

	p[0] = 4;
	p[1] = 2;
	p[2] = 6;
	p[3] = 8;
	p[4] = 2;
	p[5] = 1;
	p[6] = 7;
	p[7] = 1;
	p[8] = 2;
	p[9] = 1;

	int i = getint(p);

	printf("The value is %d", i);
		
	return 0;
}

1. The problem is that you have declared getch and ungetch() but not defined it anywhere, so define it also.
2. You are mixing C++ with C, I think new opeartor is used with C++ but not with C.

This article has been dead for over six months. Start a new discussion instead.