gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:30:10 25 4
gpt4 key购买 nike

我试图使用选项卡分隔每个衬衫尺寸,将枚举中的所有内容打印在一行中。当我尝试编译程序时,它打印出随机数而不是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com