ttyname() - question

Carlos Bazzarella bazza at hppad.HP.COM
Wed Aug 30 23:03:15 AEST 1989


Hello,

I was wondering why this work:

--------------------------------------
#include <stdio.h>
main()
{
	char *term;
	char *ttyname();
	term=ttyname();
	printf("%d",term);
}
---------------------------------------

And why this don't:
--------------------------------------
#include <stdio.h>
main()
{
	test();
}

test()
{
	char *term;
	char *ttyname();
	term=ttyname();
	printf("%d",term);
}
----------------------------------------


The second one doesn't work because when the 
compiler gets to main() and sees test(), he doesn't
know what to do with it, since it was not 
previously defined or compiled. there are 2 things 
you can do. first you could move the test() function
prior to main(), that way by the time main() is 
compiled, test() will the ready. second you could 
declared test() on top of your program or in a include
file, so when main() is compiled, test() will be given
a function overhead, that way the linker will know what
to do with test().
a function declaration, for your case, looks like this:
void test();
  3    1 2 
/* it reads: test (1) is a function (2) with no
 * returning value (void (2))
 */


			Carlos Bazzarella.
		     University of Waterloo.



More information about the Comp.lang.c mailing list