If you’re on using a glibc, then:
#define _GNU_SOURCE
#include <errno.h>
extern char *program_invocation_name;
extern char *program_invocation_short_name;
See program_invocation_name(3)
Under most Unices, __progname
is also defined by the libc.
The sole portable way is to use argv[0]