inet_addr() — Translate an Internet address into network byte order

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

X/Open:
#define _XOPEN_SOURCE_EXTENDED 1
#include <arpa/inet.h>

in_addr_t inet_addr(const char *cp);
Berkeley sockets:
#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

unsigned long inet_addr(char *cp);

General description

The inet_addr() function interprets character strings representing host addresses expressed in standard dotted-decimal notation and returns host addresses suitable for use as an Internet address.

Parameter
Description
cp
A character string in standard dotted-decimal (.) notation.
Values specified in standard dotted-decimal notation take one of the following forms:
a.b.c.d
a.b.c
a.b
a

When a 4-part address is specified, each part is interpreted as a byte of data and assigned, from left to right, to one of the 4 bytes of an Internet address.

When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the two rightmost bytes of the network address. This makes the three-part address format convenient for specifying class-B network addresses as 128.net.host.

When a two-part address is specified, the last part is interpreted as a 24-bit quantity and placed in the three rightmost bytes of the network address. This makes the two-part address format convenient for specifying class-A network addresses as net.host.

When a one-part address is specified, the value is stored directly in the network address space without any rearrangement of its bytes.

Numbers supplied as address parts in standard dotted-decimal notation can be decimal, hexadecimal, or octal. Numbers are interpreted in C language syntax. A leading 0x implies hexadecimal; a leading 0 implies octal. A number without a leading 0 implies decimal.

The address must be terminated with a null or other white space character to be valid. Any or all of the parts of the address may be empty strings. Each empty part resolves to the value 0. Input character strings that begin with a null or other white space character is treated as an empty address and will result in the value INADDR_ANY being returned.

Special behavior for C++: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.

Notes:
  1. To provide an ASCII input/output format for applications using this function, define the feature test macro __LIBASCII as described in topic __LIBASCII.
  2. The inet_addr() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Returned value

If successful, inet_addr() returns the Internet address in network byte order.

If the input character string is not in the correct format, the value INADDR_NONE is returned and errno is set to EINVAL.

Related information