Find controlling terminal with ttyname

You can identify the path the the controlling terminal device path for your current process in C with the ttyname() system call. This controlling terminal can be a virtual console (/dev/ttyn) or a pseudo-terminal (/dev/pts/n).
The ttyname system call takes the following format.
char *ttyname(int fd);
The stdin, stdout, stderr file descriptors unless redirected are by default [...]

Find IP address from remote end of a TCP socket

In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket.
The following example shows how to do this using the getpeername() and inet_ntoa() system calls.

int sockfd;
int len;
char * hostip;
struct sockaddr_in sin;

len = sizeof(sin);

if (0 != getpeername(sockfd, (struct sockaddr *) &sin, &len))
[...]

Display hostname and IP address in C

Here is a good way to determine the hostname and IP address of the local machine in C.
You first have to grab the hostname with gethostname().

char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));

Take the hostname and use it to grab the hostent struct with gethostbyname().

struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);

Finally you have to take the hostent that is returned and pull out [...]

Format output using printf

printf and its family of functions allow you to easily format string data. There are so many formatting options that it is easy to miss some of its more powerful functionality. Here is a rundown of the more useful features and some examples.
The basic printf format.

printf(format string, format parameter list);

The format string is composed [...]