gethostbyname() is a legacy C library function that resolves a host name to a struct hostent, but it is obsolete and limited. For new forward-lookup code, use getaddrinfo(), which supports modern address-family selection and returns results in caller-owned structures.
What does gethostbyname() do?
Declared in <netdb.h>, gethostbyname() accepts a host name and returns a pointer to a struct hostent describing the host. The resolver uses the system’s configured name-service sources, which can include DNS, /etc/hosts, and NIS/YP. On Linux, resolver behavior can be affected by /etc/host.conf, /etc/hosts, and /etc/nsswitch.conf (Linux man-pages: gethostbyname(3); LSB: baselib_gethostbyname(3)).
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Forvencer Server Book, 2 Zipper Pocket, Server Books for Waitress | $6.64 | Buy on Amazon |
| 2 |
|
DNS and BIND (5th Edition) | $38.88 | Buy on Amazon |
| 3 |
|
Domain Name Server (DNS) Fundamentals: Exploring Traceroute, DNS Attacks and Beyond | $14.99 | Buy on Amazon |
What the returned structure contains
h_name: the host’s official name.h_aliases: a list of alternate names.h_addrtype: the address family.h_length: the length of an address.h_addr_list: a list of addresses for the host.
The function is oriented around IPv4. When given an IPv4 address in dotted-decimal form, the documented behavior is to return that address in the host entry without performing a name lookup. For reverse lookup of an address, the corresponding legacy function is gethostbyaddr().
Why is gethostbyname() deprecated?
The Linux man-pages Library Functions Manual, 2026 edition, marks gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() obsolete. POSIX.1-2001 had already marked gethostbyname(), gethostbyaddr(), and h_errno obsolescent; POSIX.1-2008 removed those specifications and recommended modern alternatives (Linux man-pages: gethostbyname(3)).
#1 Best Overall
- Upgraded Two Zipper Pockets: Forvencer server books feature two secure zipper pockets for better organization of coins, cash, and receipts, ensuring that everything you collect has a safe and secure place
- Smart Storage & Quick Access: Designed with 8 multi-functional compartments, the right side includes a guest receipt pad, while the left has a money pocket, ticket pocket, and credit card slot. Two small clear pockets store bills, receipts, and other visible items. A stitched pen loop ensures you always have your favorite pen ready
- High-quality & Easy to Clean: Crafted from high-quality PU leather with heavy-duty stitching, this server book is built to last. It resists tears, scratches, and its waterproof surface makes cleaning easy with just a damp cloth or a non-chlorine sanitizer
- Perfect Fit for Your Apron: Measuring 5” x 8”, this compact organizer is slightly smaller than other models, making it ideal for bending or sitting while carrying in your server apron. It holds everything a waitress needs—a place for everything
- What's Included: This server organizer comes with multiple open and zippered pockets to store money, receipts, tips, etc. Clear sleeves are perfect for keeping menus or special lists while serving. Available in a variety of colors, allowing you to express yourself even when in uniform
It is not designed for modern address-family needs
The traditional interface is IPv4-oriented and does not provide the family-selection flexibility needed for code that may need IPv4, IPv6, or both. getaddrinfo() lets the caller specify address-family preferences and returns a list of suitable socket addresses.
Its result storage is not safely owned by the caller
Non-reentrant legacy functions can return pointers to static storage that a later call overwrites. Copying only the struct hostent itself does not preserve the result, because its fields point to other memory. Reentrant variants exist, but they do not make this API the preferred choice for new code.
Rank #2
Its errors use a legacy mechanism
A null return indicates failure; inspect h_errno for the error class. The documented values include HOST_NOT_FOUND (unknown host), NO_DATA/NO_ADDRESS (a valid name with no address), NO_RECOVERY (a nonrecoverable resolver failure), and TRY_AGAIN (a temporary authoritative-server failure). Modern resolution uses getaddrinfo() return codes and gai_strerror() for readable diagnostics.
What should replace it?
Use getaddrinfo() for forward hostname resolution. It supports address-family selection and avoids the legacy interface’s static-result-storage behavior. Use getnameinfo() when converting a socket address to a name or other presentation form, and gai_strerror() to turn a getaddrinfo() error code into text (Linux man-pages: getaddrinfo(3); Linux man-pages: getnameinfo(3); Linux man-pages: gai_strerror(3)).
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitches| Concern | gethostbyname() |
getaddrinfo() |
|---|---|---|
| Address families | Legacy IPv4-oriented behavior. | Caller can select address-family preferences. |
| Result storage | Non-reentrant calls may return pointers to static storage that later calls overwrite. | Returns a result list that the caller releases with freeaddrinfo(). |
| Error handling | Null result and h_errno. |
Returns an error code; use gai_strerror() for its message. |
| Name and alias fields | Returns h_name and h_aliases in struct hostent. |
Can return a canonical name when requested through hints; it does not provide the same host-alias list interface. |
| Resolver configuration | Uses system resolver configuration. | Uses system resolver configuration. |
How do you resolve a hostname in C?
Call getaddrinfo() with a hostname and service, examine the returned address list, then release it with freeaddrinfo(). This example requests IPv4 or IPv6 stream addresses and reports failures using gai_strerror():
#define _POSIX_C_SOURCE 200112L
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct addrinfo hints = {0};
struct addrinfo *results = NULL;
int status;
if (argc != 2) {
fprintf(stderr, "Usage: %s hostnamen", argv[0]);
return 2;
}
hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* stream-socket addresses */
status = getaddrinfo(argv[1], NULL, &hints, &results);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %sn", gai_strerror(status));
return 1;
}
for (struct addrinfo *p = results; p != NULL; p = p->ai_next) {
/* Use p->ai_addr and p->ai_addrlen with socket APIs. */
}
freeaddrinfo(results);
return 0;
}
AF_UNSPEC asks for results from either IPv4 or IPv6; use AF_INET or AF_INET6 in hints.ai_family when the application needs one family only. The returned list can contain multiple addresses, so code should not assume there is exactly one result. For connection code, try suitable entries in turn rather than treating the first address as guaranteed to work.
When might existing code still use it?
Existing programs may retain gethostbyname() for compatibility, especially when constrained to older IPv4-oriented interfaces. Do not treat its returned structure as durable across subsequent resolver calls, and do not assume the interface is appropriate for IPv6 or thread-safe use. For maintained or new code, migrate forward lookups to getaddrinfo(); use getnameinfo() for reverse/presentation needs.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →

