c - C语言中如何打印enum中的所有内容?

标签 c arrays for-loop enums

我试图使用选项卡分隔每个衬衫尺寸,将枚举中的所有内容打印在一行中。当我尝试编译程序时,它打印出随机数而不是 S、M、L、XL。

这是我的程序:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

enum SIZE{S,M,L,XL};

  int i,j;

int main()
{
enum SIZE s;

int t[4][4]={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};

  int *p=t;

/********DISPLAY ARRAY******************/

printf("This is our inventory \n");

  for(s=S; s<=XL; s=(s+1))
  {
      printf("%d \t", t[s]);
  }

   printf("\n");

 for( i=0; i<4; i++)
 {
    for(j=0;j<4; j++)
      {
            printf("%d\t", *p++);
      }


    printf("\n");
 }
/*****************************************/

printf("\n\n");
system("PAUSE");
return 0;
}
<小时/>
output:

This is our inventory
2686668         2686684         2686700         2686716
1       2       3       4
5       6       7       8
9       1       2       3
4       5       6       7

按任意键继续。 。 .

<小时/>

我不明白为什么打印出以下行:

2686668         2686684         2686700         2686716

相对于:

S             M               L              XL

有人可以解释一下是什么原因导致这种情况发生吗?

最佳答案

C 中的枚举没有名称(参见 this tutorial )

试试这个:How to convert enum names to string in c

关于c - C语言中如何打印enum中的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32758131/

相关文章:

c - 数组元素是 C 中字符串数组中的变量

java - 在 for 循环开始时清空 List<String>

java - 使用嵌套 for 循环的简单 Java 金字塔

c - 在 C 中使用 strncat 时出现段错误

使用 read() 函数时检查 EOF

c - 在 C 中解析逗号分隔的文件

c - 在 C 中读取 .csv 文件

arrays - 无法调用 Oracle 函数

javascript - 使用 reverse() javascript 求和两个最高值

javascript - 在不知道键名的情况下访问 JSON 对象的元素