C Program to Display Hostname and IP Address

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

A hostname is a distinct name or label assigned to any device connected to a computer network, while An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network. Each host has its unique IP address by which they are connected to the network. So let's implement a C program to find the name of the host and its IP address.

Introduction

A hostname is a distinct name or label assigned to any device connected to a computer network. It makes it easier to distinguish between different computers or devices that are linked to the Internet, a network, or both. A hostname can be alphabetic or alphanumeric, and it might include a few symbols used to identify a specific network node or device. The syntax and requirements of a hostname differ depending on the naming system in use. For example, when used in the Domain Name System (DNS) or on the Internet, a hostname must include the top-level domain name (TLD) and be between one and 63 characters long.

An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network. It is a set of rules that governs the format of data transmitted over the Internet or a local network. Each host has its unique IP address by which they are connected to the network. In this article, we will find the IP address and the hostname using a C program.

Sharpen Your Fundamentals with Free Learning

Implementation

Function definition

FunctionDescription
gethostname()This function specifies the standard host name for the local computer.
gethostbyname()This function retrieves host information from the host database corresponding to a hostname.
inet_ntoa()This function transforms an IPv4 Internet network address to an ASCII string in dotted decimal format.

Example Code

Output

Explanation

Step 1. Create a character array of size one greater than the size of the maximum possible size of the hostname. Step 2. Use gethostname function to get the host's name and check if the value returned by the function is not equal to -1. Step 3. Use gethostbyname function to get the IP address. Step 4. Convert the IP address returned by the function to an ASCII string in dotted decimal format using the inet_ntoa function.

Note:- In C and C++, perror is used to print an error message to stderr.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Conclusion

  • A hostname is a distinct name or label assigned to any device connected to a computer network.
  • A hostname can be alphabetic or alphanumeric, and it might include a few symbols used to identify a specific network node or device.
  • An Internet Protocol or IP address is a unique identifier that identifies a device on the Internet or in a local network.
  • gethostname() function specifies the standard host name for the local computer.
  • gethostbyname() function retrieves host information from the host database corresponding to a hostname.
  • inet_ntoa() function transforms an IPv4 Internet network address to an ASCII string in dotted decimal format.