
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Program to display hostname and IP address C
In this section we will see how to see the Host name and IP address of the local system in an easier way. We will write a C program to find the host name and IP.
Some of the following functions are used. These functions have a different task. Let us see the functions and their tasks.
Function | Description |
---|---|
gethostname() | It finds the standard host name for the local computer. |
gethostbyname() | It finds the host information corresponding to a host name from host database |
iten_ntoa() | It converts an IPv4 Internet network address into an ASCII string into dotted decimal format. |
Example
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> void check_host_name(int hostname) { //This function returns host name for local computer if (hostname == -1) { perror("gethostname"); exit(1); } } void check_host_entry(struct hostent * hostentry) { //find host info from host name if (hostentry == NULL) { perror("gethostbyname"); exit(1); } } void IP_formatter(char *IPbuffer) { //convert IP string to dotted decimal format if (NULL == IPbuffer) { perror("inet_ntoa"); exit(1); } } main() { char host[256]; char *IP; struct hostent *host_entry; int hostname; hostname = gethostname(host, sizeof(host)); //find the host name check_host_name(hostname); host_entry = gethostbyname(host); //find host information check_host_entry(host_entry); IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0])); //Convert into IP string printf("Current Host Name: %s
", host); printf("Host IP: %s
", IP); }
Output
Current Host Name: soumyadeep-VirtualBox Host IP: 127.0.1.1
- Related Articles
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Python program to Display Hostname and IP address?
- Java program to display Hostname and IP address
- C Program to validate an IP address
- How to display the IP Address of the Machine using C#?
- Program to convert IP address to hexadecimal in C++
- Difference between Static IP Address and Dynamic IP Address
- C# program to find IP Address of the client
- JavaScript program to retrieve clients IP address
- C Program to find IP Address, Subnet Mask & Default Gateway
- Display the Host Name and IP Address on a Tkinter Window
- Difference between IP Address and MAC Address
- Difference between MAC Address and IP Address
- Validate IP Address in C#

Advertisements