0

A 3rd party library have a list of defined status variables in a header

// <status.h> -- 3rd party header file
#define SUCCESS 0
#define FAILURE 1
#define OUT_OF_MEM 2
// ... and a lot of them ...

// Functions that return the above status
int Send(); 

I want to display the status names, i.e. those defined variable names

// "main.c"
#include <status.h>
#include <stdio.h>

void printstat(stat)
{  // Print out stat with variable name
   // Example if 0, print "SUCCESS", and so on... 
}

void main()
{   
   int stat = Send();  
   printstat(stat);
}

Because too much define status variables, so what is the easy way to do that?

hongcc
  • 45
  • 6
  • 1
    possible duplicate of [Printing name and value of a define](http://stackoverflow.com/questions/1164652/printing-name-and-value-of-a-define) – Jongware Oct 19 '14 at 11:19
  • To create a reverse lookup table by reading the status.h in the program. – BLUEPIXY Oct 19 '14 at 11:28

1 Answers1

0

There is no "direct" solution. After preprocessing phase of translation unit those names are simply gone (i.e. replaced with their values). The simplest way would be to create addition array of string literals, where index of each element corresponds to macro's value. For example:

const char *status_name[] = {"SUCCESS", "FAILURE", "OUT_OF_MEM"};

The best place to place such array is header file itself. Note that you need to synchronize header with array. If it changes frequently, then you possibly need some sort of auto-generation.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137