Get signal name from signal number I am trying to get a signal name to print when getting a signal number.
I actually have :
char* getsig(int sig){
    switch (sig)
    {
    case SIGKILL:
        return &quot;SIGKILL&quot;;
    case SIGSTOP:
        return &quot;SIGSTOP&quot;;
    case SIGTERM:
        return &quot;SIGTERM&quot;;
    case SIGTRAP:
        return &quot;SIGTRAP&quot;;
    case SIGABRT:
        return &quot;SIGABRT&quot;;
    case SIGALRM:
        return &quot;SIGALARM&quot;;
    case SIGSEGV:
        return &quot;SIGSEGV&quot;;
    case SIGQUIT:
        return &quot;SIGQUIT&quot;;
    case SIGINT:
        return &quot;SIGINT&quot;;
    case SIGCHLD:
        return &quot;SIGCHLD&quot;;
    case SIGCONT:
        return &quot;SIGCONT&quot;;
    case SIGPIPE:
        return &quot;SIGPIPE&quot;;
    case SIGFPE:
        return &quot;SIGFPE&quot;;
    case SIGILL:
        return &quot;SIGILL&quot;;
    case 0:
        return &quot;[NO SIGNAL TO DELIVER]&quot;;
    default:
        return &quot;UNKN&quot;;
    }
}

I can see that this is wrong : I have no certitude that those char* names will be stored in data section on every compiler.
I am certain that there is a clean way of doing such a thing, but I cannot find it on the internet. Do anyone has an idea of how I could do this task the beautiful way?
Edit
The final aim is to print a potential signal delivered by a ptrace(PTRACE_CONT, ...); call, e.g:
printf(&quot;ptrace(PTRACE_CONT); [delivered %s]\n&quot;, getsig(data));
The way to do this in C is to define an array, and use designated array initializers to ensure the correct mapping between number and string, like so:
static const char *signames[] = {
    [SIGKILL] = &quot;SIGKILL&quot;,
    [SIGSTOP] = &quot;SIGSTOP&quot;,
    ...
    [0] = &quot;[NO SIGNAL TO DELIVER]&quot;,
};

const char *getsig(int sig) {
    if (sig &lt; 0 || sig &gt;= sizeof(signames) / sizeof(*signames) || !signames[sig])
        return &quot;UNKN&quot;;

    return signames[sig];
}

Note that it is hard to make this portable; the list of available signals varies by operating system, and some don't even have the concept of signals. Some operating systems and/or standard libraries might already have some function to convert signal numbers to strings, if you target a specific platform and don't care about compatibility, you might consider using that.
Note that strsignal() should be available on any POSIX platform, although it gives you a more human readable string like &quot;Killed&quot; instead of &quot;SIGKILL&quot;.