Can I display the value of an enum with printf()?

CPrintfEnums

C Problem Overview


Is there a one-liner that lets me output the current value of an enum?

C Solutions


Solution 1 - C

As a string, no. As an integer, %d.

Unless you count:

static char* enumStrings[] = { /* filler 0's to get to the first value, */
                               "enum0", "enum1", 
                               /* filler for hole in the middle: ,0 */
                               "enum2", "enum3", .... };

...

printf("The value is %s\n", enumStrings[thevalue]);

This won't work for something like an enum of bit masks. At that point, you need a hash table or some other more elaborate data structure.

Solution 2 - C

enum MyEnum
{  A_ENUM_VALUE=0,
   B_ENUM_VALUE,
   C_ENUM_VALUE
};


int main()
{
 printf("My enum Value : %d\n", (int)C_ENUM_VALUE);
 return 0;
}

You have just to cast enum to int !
Output : My enum Value : 2

Solution 3 - C

The correct answer to this has already been given: no, you can't give the name of an enum, only it's value.

Nevertheless, just for fun, this will give you an enum and a lookup-table all in one and give you a means of printing it by name:

main.c:

#include "Enum.h"

CreateEnum(
		EnumerationName,
		ENUMValue1,
		ENUMValue2,
		ENUMValue3);

int main(void)
{
	int i;
	EnumerationName EnumInstance = ENUMValue1;

    /* Prints "ENUMValue1" */
    PrintEnumValue(EnumerationName, EnumInstance);

    /* Prints:
     * ENUMValue1
     * ENUMValue2
     * ENUMValue3
     */
	for (i=0;i<3;i++)
	{
		PrintEnumValue(EnumerationName, i);
	}
	return 0;
}

Enum.h:

#include <stdio.h>
#include <string.h>

#ifdef NDEBUG
#define CreateEnum(name,...) \
	typedef enum \
	{ \
		__VA_ARGS__ \
	} name;
#define PrintEnumValue(name,value)
#else
#define CreateEnum(name,...) \
	typedef enum \
	{ \
		__VA_ARGS__ \
	} name; \
	const char Lookup##name[] = \
		#__VA_ARGS__;
#define PrintEnumValue(name, value) print_enum_value(Lookup##name, value)
void print_enum_value(const char *lookup, int value);
#endif

Enum.c

#include "Enum.h"
				
#ifndef NDEBUG
void print_enum_value(const char *lookup, int value)
{
	char *lookup_copy;
	int lookup_length;
	char *pch;

	lookup_length = strlen(lookup);
	lookup_copy = malloc((1+lookup_length)*sizeof(char));
	strcpy(lookup_copy, lookup);

	pch = strtok(lookup_copy," ,");
	while (pch != NULL)
	{
		if (value == 0)
		{
			printf("%s\n",pch);
			break;
		}
		else
		{
			pch = strtok(NULL, " ,.-");
			value--;
		}
	}

	free(lookup_copy);
}
#endif

Disclaimer: don't do this.

Solution 4 - C

enum A { foo, bar } a;
a = foo;
printf( "%d", a );   // see comments below

Solution 5 - C

Some dude has come up with a smart preprocessor idea in this post

https://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c

Solution 6 - C

I had the same problem.

I had to print the color of the nodes where the color was: enum col { WHITE, GRAY, BLACK }; and the node: typedef struct Node { col color; };

I tried to print node->color with printf("%s\n", node->color); but all I got on the screen was (null)\n.

The answer bmargulies gave almost solved the problem.

So my final solution is:

static char *enumStrings[] = {"WHITE", "GRAY", "BLACK"};
printf("%s\n", enumStrings[node->color]);

Solution 7 - C

Printing an enum value can be tricky as the sizes of each of its members can vary depending on the implementation. Take this example compiled on gcc 8.4.0.

#include <stdio.h>

int main(void) {
  enum option {A = 0, B = 0x100000000};

  // Enumerator sizes of the same enumeration can differ
  printf("sizeof(A)=%zu\n", sizeof(A)); // sizeof(A)=4
  printf("sizeof(B)=%zu\n", sizeof(B)); // sizeof(B)=8

  // Same output even though they have different values
  printf("A=%d\n", A); // A=0
  printf("B=%d\n", B); // B=0

  // You should know beforehand the maximum enumerator size
  printf("A=%ld\n", A); // A=0
  printf("B=%ld\n", B); // B=4294967296
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPieterView Question on Stackoverflow
Solution 1 - CbmarguliesView Answer on Stackoverflow
Solution 2 - CMatthieuView Answer on Stackoverflow
Solution 3 - CDrAlView Answer on Stackoverflow
Solution 4 - CanonView Answer on Stackoverflow
Solution 5 - CBlueTrinView Answer on Stackoverflow
Solution 6 - CAlex BondorView Answer on Stackoverflow
Solution 7 - CMateo de MayoView Answer on Stackoverflow