The following program and its output show that INET_ADDRSTRLEN is defined as 16 , and INET_ADDRSTRLEN is defined as 46 .
Here is the program.
#include <stdio.h> #include <arpa/inet.h> int main() { printf("%d\n", INET_ADDRSTRLEN); printf("%d\n", INET6_ADDRSTRLEN); return 0; }
Here is the result.
16 46
I can understand why INET_ADDRSTRLEN should be 16 . The largest possible string representation of an IPv4 address consumes 15 bytes, for example. "255.255.255.255" . Therefore, 16 bytes is required to store such an IP address with its null character.
But why should INET6_ADDRSTRLEN be 46 ? The maximum possible string representation of an IPv6 address consumes only 39 bytes (according to my knowledge), for example. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" . Therefore, only 40 bytes with its trailing null character are required to store such an IP address.
Is there a string representation of an IPv6 address that can consume 46 bytes?
c sockets ipv6
Lone learningner
source share