Search code examples
cnetwork-programmingposix

IF_NAMESIZE vs. IF_NAMESIZE+1


When I use IF_NAMESIZE (from net/if.h in libc implementations) as array size, should I use it as it is or with + 1 for \0 (null byte)?

char iface[IF_NAMESIZE];

or

char iface[IF_NAMESIZE + 1];

I see it using both ways across various open source projects.


Solution

  • The header shall define the following symbolic constant for the length of a buffer containing an interface name (including the terminating NULL character):

    IF_NAMESIZE Interface name length.

    http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/net_if.h.html

    So:

    char iface[IF_NAMESIZE];
    

    is enough